PlugIn Impersonation In Microsoft Dynamics CRM 4.0(i.e Executing plugin code on behalf of another user.)

Posted: January 20, 2009 in Microsoft Dynamics CRM
Tags: ,

If we want to execute some code in behalf of another user not the calling user then we have to handle this in PlugIn code. Whatever the user selected during PlugIns registration, it doesn’t matter, code will alwaysexecute on behalf of user mentioned in code.Example Create a Record in CRM on behalf of another user.




                                CrmSdk.CrmAuthenticationToken token = new CrmSdk.CrmAuthenticationToken();

                                token.AuthenticationType = 0;

                                token.OrganizationName = OrganizationName;



                                // Create CrmService instance

                                CrmSdk.CrmService service= new CrmSdk.CrmService();

                                service.Url = CrmServiceUrl;

                                service.CrmAuthenticationTokenValue = token;


                                //execute on behalf of another user

                                service.Credentials = new System.Net.NetworkCredential(“username”,”password”,”domain”);

                                //service.CorrelationTokenValue = correlationToken;

                                //Create Proposal record

                                CrmSdk.new_proposal proposal = new CrmSdk.new_proposal();


                                // set value of Opportunity Lookup in Proposal entity.

                                CrmSdk.Lookup oppLookup = new CrmSdk.Lookup();

                                oppLookup.type = EntityName.opportunity.ToString();

                                oppLookup.Value = new Guid(oppId);


                                proposal.new_opportunityid = oppLookup;



                            catch (SoapException ex)


                               // show message here.. 



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s