Posts Tagged ‘IOrganizationService’

Creating OrganizationServiceProxy in CRM2011 custom applications

February 26, 2012 7 comments


Below are the steps to instantiate “Organization Service Proxy” which can be used to consume CRM service and perform operations in a  custom application

– Refer “Microsoft.Xrm.Sdk & Microsoft.Xrm.Sdk.Client” .dll’s to your custom application

using Microsoft.Xrm.Sdk;

using Microsoft.Xrm.Sdk.Client;

using System.Net.Security;

using System.ServiceModel.Description;

using System.Security.Cryptography.X509Certificates;

private Uri homeRealmUri = null;

private ClientCredentials credentials;

private Uri organizationUri;

private IOrganizationService service;

private OrganizationServiceProxy serviceProxy;

 credentials = new ClientCredentials();

// If CRM On-Premise

// To use custom credentials pass user credentials

credentials.Windows.ClientCredential = new NetworkCredential( {userId},{ password},{ domain} );

// To use Default Credentials; Uncomment below line and comment above

// credentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials;

// If CRM Online

credentials.UserName.UserName = “Office 365 ID”; // i.e.,

credentials.UserName.Password = “Office 365 Password”;

// Use “https” if CRM is SSL configured

string orgUrl= “http://ServerName/OrganizationName/XRMServices/2011/Organization.svc”;

// Add this line if URL has “https” (i.e.,CRM is SSL configured)

if (!string.IsNullOrEmpty(orgUrl) && orgUrl.Contains(“https”)) {

ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain,
SslPolicyErrors sslPolicyErrors) { return true; };


organizationUri = new Uri(orgUrl);

using (

serviceProxy =

new OrganizationServiceProxy(organizationUri, homeRealmUri, credentials, null))


// To impersonate set the GUID of CRM user here

serviceProxy.CallerId = {GUID of CRM User};

serviceProxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());

service = serviceProxy;


Hope it helps 🙂