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:
2. Add a plugin in the update stage of Test Entity
3. In this plugin, the code snippets of core part are:
5. Run plugin again and it will change the ownership:
• Whereas the “InitiatingUserId” is the ID of normal CRM user whose action triggers plugin at the first place.
• 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:
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):
4. Now, uncomment 2nd line and comment out the 1st line so we use IExecutionContext.InitiatingUserId5. Run plugin again and it will change the ownership:
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.