Friday, February 12, 2010

Impersonate CrmService web service calls by using CallerId

At times users access a resource as though they were someone else. This is known as impersonation. For example, in CRM, when we tried retrieved "saved views" from userquery entity, you can only retrieve those views that are belongs to you even though you are using CRM Admin account to retrieve those information.

Below is the code sample to impersonate CrmService calls in CRM 4.0:
CrmAuthenticationToken token = new CrmAuthenticationToken();
token.OrganizationName = _strOrganization;
token.AuthenticationType = 0;
token.CallerId = userid; // userid that you want to impersonate

CrmService crmService = new CrmService();
crmService.Url = _strServerUrl + "/MSCRMServices/2007/crmservice.asmx";
crmService.CrmAuthenticationTokenValue = token;

// You can use System.Net.CredentialCache.DefaultCredentials
crmService.Credentials = new NetworkCredential("username", "password", "domain");

Caution: The credential user must be a member of PrivUserGroup. (You can check it under Active Directory Users and Computer) If is not assigned properly, you will receive following error message:
error code: 0x80040204
description: Invalid user auth.
error type: Platform


Rami Heleg said...

Hi Jeffery,
i tried to use your example. my admin exist in privusergroup + if i use network creditial works fine but if i use CalledId still failed to make the action for instance create, update and get error 401.

any idea?
Rami Heleg

Anonymous said...

any comments regarding my question/problem?

Thuta Hlaing said...


Can you try following code in your program?

CrmAuthenticationToken token = new CrmAuthenticationToken();
token.OrganizationName = _orgName;
token.AuthenticationType = 0; // AD
token.CallerId = _userid;

CrmService oCrmService = new CrmService();
oCrmService.Url = _serverURL; //crmservice.asmx
oCrmService.CrmAuthenticationTokenValue = token;

// try with this code, you don't need network credential
oCrmService.UseDefaultCredentials = true;

Hope it helps!


Gandhi said...

How can I retrieve the userid GUID from CRM if I know the full name of the that CRM user?

Post a Comment