Showing posts with label Impersonation. Show all posts
Showing posts with label Impersonation. Show all posts

Thursday, October 20, 2011

Impersonation in CRM2011

Impersonate in Plugins:

ICrmService service = context.CreateCrmService(context.InitiatingUserId);
Instead of context.InitiatingUserId use the user guid of the user.

ICrmService service = context.CreateCrmService(userId);

To Impersonate in WebPages:
Create crmservice and assign callerid property to the user to be impersonated.

Thursday, November 18, 2010

Impersonation in custom aspx page for logged in user

To execute crmservice calls from the logged in user context




CrmService crmservice = new CrmService();
            string orgName = "contoso";

            using (new CrmImpersonator())
            {
                CrmAuthenticationToken token = CrmAuthenticationToken.ExtractCrmAuthenticationToken(Context, orgName);
                token.AuthenticationType = 0;
                crmservice.Url = "http://crm//MSCRMServices//2007//crmservice.asmx";
                crmservice.CrmAuthenticationTokenValue = token;
                crmservice.Credentials = CredentialCache.DefaultCredentials;




                try
                {
                    //Any crmservice call 
                   //crmservice.Execute
                }
                catch (Exception ex)
                {
                }


            }


Remember that the calls have to be executed within the using block.
Else, it results in an exception.