Dynamics 365 – Impersonate a User in Plugin

Leave a comment

In Dynamics 365, when testing plugins, you can change which user the plugin runs under. Do to this, open the step in the Plugin Registration Tool. We have a plugin that runs on create of an account, that creates a new Task. We will select this to run under the context of a user, Alan Steiner by setting Run in User’s Context:

Our user Alan Steiner does not have privileges to create a new task in the system:

So what happens when we try to create a new task (as either Alan or any other user)? Create a new account:

Our plugin code will run. Now go to System Jobs. You will see the plugin failed to execute:

Open the job, and you will see the details show the user does not have access to the prvCreateActivity privilege:

Set the plugin back to the Calling User to run as normal:

Note you can also impersonate a user within your plugin code by using

serviceFactory.CreateOrganizationService(UserGuid);

From the documentation: “When called in a plug-in, a null value indicates the SYSTEM user and a Guid.Empty value indicates the same user as IPluginExecutionContextUserId. Any other value indicates a specific system user.”

ABOUT CARL DE SOUZA

Carl de Souza is a developer and architect focusing on Microsoft Dynamics 365, BI, Web, Cloud and Data Science.

carldesouza.comLinkedIn Twitter | YouTube

 

Leave a Reply

Your email address will not be published. Required fields are marked *