Development Desk

Just another WordPress.com site

Monthly Archives: December 2015

Connecting to CRM Online (2013/2015)

Whenever i am going to extend CRM system or developing 3rd party party for CRM or a webservice. CRM Authentication is required to perform Create, Update, Delete or retrieve operations in CRM organisation therefore connection must be established and authenticated correctly to get hands on the CRM IOrganisation Service. Below is simple code to connect to CRM. i wrote this example in C# using windows console application and reading credentials from app.config.

</pre>
<pre class="lang-cs prettyprint prettyprinted"><code><span class="pln">public static class CrmConnector
 {
 public static IOrganizationService CreateCrmService()
 {
 ConnectToMscrm();

 var userid = ((WhoAmIResponse)_orgService.Execute(new WhoAmIRequest())).UserId;
 if (userid != Guid.Empty)
 {
 Console.WriteLine("Connection Established Successfully");
 Console.ReadKey();
 }
 else
 {
 Console.WriteLine("Connection couldn't established Successfully");
 Console.ReadKey();
 }
 
 return _orgService;
 }

 static IOrganizationService _orgService;

 public static void ConnectToMscrm()
 {
 try
 {
 var connectorUser = ConfigurationManager.AppSettings["connectorUser"]; //test.user@crmorg.onmicrosoft.com
 var connectorPassword = ConfigurationManager.AppSettings["connectorPassword"]; //password123
 var uri = ConfigurationManager.AppSettings["crmOrgServiceUrl"]; //https://crmorg.api.crm4.dynamics.com/XRMServices/2011/Organization.svc
 //var homeRelamUrl = new Uri(ConfigurationManager.AppSettings["HomeRelamURL"]);


 var credentials = new ClientCredentials();
 credentials.UserName.UserName = connectorUser;
 credentials.UserName.Password = connectorPassword;

 var serviceUri = new Uri(uri);

 var orgProxy = new OrganizationServiceProxy(serviceUri, null, credentials, null);
 orgProxy.EnableProxyTypes();
 _orgService = (IOrganizationService)orgProxy;
 }
 catch (Exception ex)
 {
 //throw new Exception("Error Has Occurred while connecting to CRM Server " + ex.Message);
 Console.WriteLine("Error Has Occurred while connecting to CRM Server " + ex.Message);
 }
 }
 } 
</span></code></pre>
<pre>
 
Advertisements