In this Blog, I want 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]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):
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”.1. MSDN, http://msdn.microsoft.com/en-us/library/microsoft.xrm.sdk.iexecutioncontext.userid
2. MSDN, http://msdn.microsoft.com/en-us/library/microsoft.xrm.sdk.iexecutioncontext.initiatinguserid