Posts Tagged ‘Impersonation’


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.. 

                            }