Logo
banner

Blogs

Execute a plugin in the context of the logged in user or calling user or SYSTEM user in Microsoft Dynamics 365

, March 21, 2018 23205 Views

Many a times, in Microsoft dynamics CRM, we may want to execute some part of the plugin as logged in user or calling user(As mentioned in plugin step registration) or SYSTEM user.

Today, in this blog, I will explain the UserId and InitiatingUserID properties of the iPuginExecutionContext, which are used for getting the user id, using the execution context of a plugin as well as, how we can execute the plugin in context of the SYSTEM user in dynamics crm.

UserId : It gives the user id of the calling user, set during plugin step registration. This user is the one, “on behalf of” whom the plugin is fired.

Below is an example of, how we can use the UserId property of the iPuginExecutionContext:-

I have the below plugin, fired on the create of the new record in account entity. Here, the calling user is User 2:-

And, the below code in the plugin, assigns the account entity record to the calling user:-

Entity entity = (Entity)iPluginExecutionContext.InputParameters["Target"];
EntityReference user = new EntityReference("systemuser", iPluginExecutionContext.UserId);
EntityReference entityReference = new EntityReference("account", entity.Id);

iOrganizationService.Execute(new AssignRequest
{
Assignee = user,
Target = entityReference
});

 

InitiatingUserId : It gives the user id of the user, who performs the operation, that triggers the plugin.

 


Discover the power of our services:

Microsoft Fabric, Power BI, Microsoft Business Intelligence, SQL Server, and Business Central. By the power of these services, from advanced analytics to seamless business integration, we’ve got the expertise you need to optimize operations and drive growth. Harness the potential of your data infrastructure with our comprehensive suite of solutions.


 

Below is an example of, how we can use the InitiatingUserId property of the iPuginExecutionContext:-

Suppose that, the “user1” is logged in into the crm, and the below plugin is registered with the calling user as “user2”:-

Now, the below code in the plugin, assigns the account entity record to the logged in user:-

Entity entity = (Entity)iPluginExecutionContext.InputParameters["Target"];
EntityReference user = new EntityReference("systemuser",iPluginExecutionContext.InitiatingUserId);
EntityReference entityReference = new EntityReference("account", entity.Id);

iOrganizationService.Execute(new AssignRequest
{
Assignee = user,
Target = entityReference
});

 

SYSTEM User : Suppose “user2” does not have the privilege to assign a record and the calling user is set to “user2”, while registering the step in plugin, with the below plugin execution:

IOrganizationService iOrganizationService = iOrganizationServiceFactory.CreateOrganizationService(iPluginExecutionContext.UserId);

Entity entity = (Entity)iPluginExecutionContext.InputParameters["Target"];
EntityReference user = new EntityReference("systemuser", iPluginExecutionContext.UserId);
EntityReference entityReference = new EntityReference("account", entity.Id);

iOrganizationService.Execute(new AssignRequest
{
Assignee = user,
Target = entityReference
});

 

Here, we get an error message as below, since, the user under whose context the plugin is fired, does not have the “assign” permission.

In this case, we can execute the plugin in the context of “SYSTEM”, where we must pass “null”, while creating the organization service object as below:

IOrganizationService iOrganizationService = iOrganizationServiceFactory.CreateOrganizationService(null);

Entity entity = (Entity)iPluginExecutionContext.InputParameters["Target"];
EntityReference user = new EntityReference("systemuser", iPluginExecutionContext.UserId);
EntityReference entityReference = new EntityReference("account", entity.Id);

iOrganizationService.Execute(new AssignRequest
{
Assignee = user,
Target = entityReference
});

 

Thus, the record gets assigned to the calling user “user2”.

In this way, you can execute a plugin in the context of the logged in user or calling user or SYSTEM user, as per your requirement.

I hope this helps you!!

Happy CRMing.


 

Insert data into Many-to-Many relationship in Dynamics CRM very easily & quickly, using the Drag and drop listbox.
http://www.inkeysolutions.com/what-we-do/dynamicscrmaddons/drag-and-drop-listbox

mm

Inkey

INKEY is your solution partner.
Our focus is to deliver you in-time intelligent innovative solutions ("key") for the problems in hand. Maintaining a quality standard right from the inception of a project is our top most priority.

Our team of talented professionals will execute your projects with dedication and excellence. We take ownership and accountability for the effort that goes into meeting our client’s needs.

Years of experience and proven success of delivering innovative custom solutions.

More posts by

3 responses to “Execute a plugin in the context of the logged in user or calling user or SYSTEM user in Microsoft Dynamics 365”

  1. Antony says:

    Great post one of the better explanations I’ve seen for plugin execution.

  2. Arjan says:

    Thank you! Good explanation about how to create a record as SYSTEM user.

Leave a Reply

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

The maximum upload file size: 2 MB. You can upload: image, audio, video, document, spreadsheet, interactive, text, archive, code, other. Drop file here

Would you like to digitize your business and put it on the cloud?
Do you need clear, concise reports for your organization?