web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Dynamics 365 Community / Blogs / DynamicsDevPro / MS CRM Plugin - IPluginExe...

MS CRM Plugin - IPluginExecutionContext InitiatingUserId and UserId - #PluginTip2

ram r Profile Picture ram r

Hello All,

Glad to meet you with another tip, In plugin execution context we have two attributes which points to user record with which we can create organization service. Both the attributes are of type EntityReference and at the end of this post you will have good idea on what is the difference between them and when to use specific attribute.

Initiatinguserid:
This is the user who performs the operation that triggers the plugin. If USER-A creates a record and a plugin is triggered on record create then the Initiatinguserid is USER-A.
Userid:
This contains the "run in user's context" users id that is set as a part of plugin step creation. If the value is not set, then it equivalent to Initiatinguserid
In below code, we have created orgservice with both user entityreference's
Example code:
 IPluginExecutionContext context = (IPluginExecutionContext)  
serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(
context.UserId);
IOrganizationService intiatingUserService = serviceFactory.CreateOrganizationService(
context.InitiatingUserId);

Example Scenario's
======================================================
======================================================
Scenario - 1
USER-A executes a plugin step without  "run in user's context".
Initiatinguserid/Userid  = USER-A
======================================================
Scenario - 2
USER-A  executes a plugin step with  "run in user's context" as USER-B
Initiatinguserid = USER-A
Userid = USER-B

Hope this helps!
Also check out my blog on plugin depth

This was originally posted here.

Comments

*This post is locked for comments