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 1819 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.

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

One response 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.

Leave a Reply

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