Monday, April 1, 2013

Create impersonated CRM service in CRM 2011 using CRM SDK

Please use the code below to create impersonated CRM service in CRM 2011.
 
public static IOrganizationService Service() 

{

    ClientCredentials Credentials = new ClientCredentials(); 

    Credentials.Windows.ClientCredential.UserName ="Impersonate User ID"; 

    Credentials.Windows.ClientCredential.Password ="Impersonate User ID password"; 

    //Update the below URL as per your environment

    Uri OrganizationUri = new Uri("http://serverName/OrganizationName/XRMServices/2011/Organization.svc"); 

    Uri HomeRealmUri = null; 

    //Get the CRM Service from the CRM Service Proxy

    using (OrganizationServiceProxy serviceProxy = new OrganizationServiceProxy(OrganizationUri, HomeRealmUri, Credentials, null)) 

    {

        IOrganizationService service = (IOrganizationService)serviceProxy; 

        return service; 

    }

}