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.
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
© All Rights Reserved. Inkey IT Solutions Pvt. Ltd. 2024
Great post one of the better explanations I’ve seen for plugin execution.
Thank you! Good explanation about how to create a record as SYSTEM user.
Hello Arjan,
Happy that the blog helped.