Bhoopathi

"Be Somebody Nobody Thought You could Be"

Friday, June 9

The difference between UserId and InitiatingUserId in Plugin of Dynamic CRM 2011

I would like to discuss the difference between UserId and InitiatingUserId of IExecutionContext interface in Dynamic CRM 2011 plugin. The explanations of them are given in MSDN like this:
• IExecutionContext.UserId Property: Gets the global unique identifier of the system user for whom the plug-in invokes Web service methods on behalf of. [1]
• IExecutionContext.InitiatingUserId: Gets the global unique identifier of the system user account under which the current pipeline is executing. [2]

in Simple:
The context gives us both the GUIDs
  • context.initiatinguserid: gets the systemuser GUID who actually fired the plugin
  • context.userid : gets the  impersonated systemuser GUID
From the official definition, I feel it is still not very clear. So I set up an experiment in my development environment.
1. Create a Test Entity record and assign it to another user in system:

 The difference between UserId and InitiatingUserId in Plugin of Dynamic CRM 2011
2. Add a plugin in the update stage of Test Entity

 The difference between UserId and InitiatingUserId in Plugin of Dynamic CRM 2011
3. In this plugin, the code snippets of core part are:

 The difference between UserId and InitiatingUserId in Plugin of Dynamic CRM 2011
 The difference between UserId and InitiatingUserId in Plugin of Dynamic CRM 2011
This plugin is running as a system user and assigns ownership of the Test Entity to the IExecutionContext.UserId.
If we look at the Test Entity it is now assigned to someone else (on form refresh):

 The difference between UserId and InitiatingUserId in Plugin of Dynamic CRM 2011
4. Now, uncomment 2nd line and comment out the 1st line so we use IExecutionContext.InitiatingUserId

5. Run plugin again and it will change the ownership:

 The difference between UserId and InitiatingUserId in Plugin of Dynamic CRM 2011
Now we can see the difference between the two options
• With “UserId” the plugin is running under system context, and the “UserId” gives the ID of user we define in plugin, namely “System”. It focuses on “behalf of”.

• Whereas the “InitiatingUserId” is the ID of normal CRM user whose action triggers plugin at the first place.