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

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.

 

try

                            {

                                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;

                                service.Create(proposal);

                            }

                            catch (SoapException ex)

                            {

                               // show message here.. 

                            }

Author: Arvind Singh

Microsoft Dynamics CRM Consultant

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com 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