CRM 2011 : how to use OrganizationServiceClient with pure WCF call (no XRM dll)
Below are the steps which may be helpful while trying to access CRM 2011 on premise with "OrganizationServiceClient" class. Benefits: no need to use XRM.dll, no early bound class. It will also help in accessing multiple Organisations in an CRM 2011 installation with the same piece of code without adding the .wsdl file multiple times. It is just like CRM 4.0 with multiple organisations.
Steps:
1. Add Service Request to your VS project with the format: "http://CRM ServerName:defaultPort/OrganizationName/XRMServices/2011/Organization.svc". For example "http://pxtsgo0666:5555/CRMDev/XRMServices/2011/Organization.svc".
2. Add namespace of that Service Request to your project (using).
3. Use the code below:
==================================================
string strOrg = "CRMDev";
string strURI = @"http://CRMServerName:defaultPort/" + strOrg + "/XRMServices/2011/Organization.svc";
OrganizationServiceClient orgProxy = new OrganizationServiceClient();
orgProxy.ClientCredentials.Windows.ClientCredential =new System.Net.NetworkCredential("UserId", "Password", "DomainName");
System.ServiceModel.EndpointAddress myEndpointAdd = new System.ServiceModel.EndpointAddress(new Uri(strURI), System.ServiceModel.EndpointIdentity.CreateDnsIdentity(""));
orgProxy.Endpoint.Address= myEndpointAdd;
Entity objAcc = newEntity();
objAcc.LogicalName ="account";
AttributeCollection myAttColl = new AttributeCollection();
myAttColl.Add(new KeyValuePair<string,object>("name", "AAA from UNTYPE ClientMethod"));
myAttColl.Add(new KeyValuePair<string,object>("emailaddress1", testfrom@test.com));
myAttColl.Add(new KeyValuePair<string,object>("telephone1", "+1122334455"));
objAcc.Attributes = myAttColl;
orgProxy.Create(objAcc);
=================================================
Update record:
Entity objAccUpdate = new Entity() { LogicalName = "account"};
AttributeCollection myAttCollUpd = new AttributeCollection();
myAttCollUpd.Add(new KeyValuePair<string, object>("emailaddress1", "UpdatedTestfrom@test.com"));
myAttCollUpd.Add(new KeyValuePair<string, object>("telephone1", "+4612312312"));
myAttCollUpd.Add(new KeyValuePair<string, object>("accountid", new Guid("account guid to be updated") ) );
objAccUpdate.Attributes = myAttCollUpd;
OrganizationServiceClient_Object.Update(objAccUpdate);
==============================================
Now, you can use the same piece of code to access multiple organizations within the same installation by changing the "strOrg" variable. Here an account record is being created.
Note: you must create a DNS Name separately with statement "System.ServiceModel.EndpointAddress myEndpointAdd = new System.ServiceModel.EndpointAddress(new Uri(strURI),System.ServiceModel.EndpointIdentity.CreateDnsIdentity(""));"
Here you can pass any value while creating DNS identity ( CreateDnsIdentity("xyz") ). You can use also below line if you dont want to create DNS, but want to have UPN Identity :
System.ServiceModel.EndpointAddress myEndpointAdd = new System.ServiceModel.EndpointAddress(new Uri(strURI), System.ServiceModel.EndpointIdentity.CreateUpnIdentity("1@1"), //string.Format("{0}@{1}", _user, "_domain")),
new System.ServiceModel.Channels.AddressHeader[] { });