Development Desk

Just another site

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 class="lang-cs prettyprint prettyprinted"><code><span class="pln">public static class CrmConnector
 public static IOrganizationService CreateCrmService()

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

 static IOrganizationService _orgService;

 public static void ConnectToMscrm()
 var connectorUser = ConfigurationManager.AppSettings["connectorUser"]; //
 var connectorPassword = ConfigurationManager.AppSettings["connectorPassword"]; //password123
 var uri = ConfigurationManager.AppSettings["crmOrgServiceUrl"]; //
 //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);
 _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);

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: