Fixing Plugin Not Triggered on Create of a User in Microsoft Dynamics 365

Zoe Sands, 05 March 2018

I recently had to write a plugin for a client that would be triggered when a user was created. This plugin would create some custom settings records for that user.

The first thing I tried was creating these records when a user was created however despite being able to register a plugin on create of a user, nothing I did would trigger the plugin!

The second thing I tried was to trigger the plugin when the user was added to the default team because every user is assigned to the default team automatically when they are created. Again, this did nothing.

My ultimate solution was to register the plugin on associate of a user to a security role. The role I chose was one that all users are assigned when they are created. As all users are assigned the role shortly after they are created, you could argue that this is the same as triggering a plugin on create of a user. It also meant that we could still ensure that the user would have permission to own the records that we were going to create for them.

The following shows some of the steps that I took to resolve this issue.

The plugin needs to be triggered on associate of a user to a security role. The Associate SDK message does not take any entity parameters. This must be handled inside the plugin code.

image

The following code shows how you can check that the target entity that triggered the plugin is a system user, as well as that the related record is a security role.

image

An alternative approach would be to use a workflow which runs on create of a user. You could use the workflow designer if you are doing something simple, or include custom logic for more complex scenarios.