Connect to CRM Online or On-premise using C# SDK

Follow the below Steps, to connect CRM Online or On-Premise using C#,

Step 1: Include the below References in your project, you can get the same from Latest SDK. Open SDK and goto the path, SDK –> Bin for the dlls

Microsoft.Crm.Sdk.Proxy
Microsoft.Xrm.Sdk

Step 2: Include the below Framework Assemblies in your project,

System.Runtime.Serialization
System.ServiceModel

Reference Manager - ConnectToCRM

Step 3: Finally add the below namespaces in your class file,

using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using System.ServiceModel.Description;

Your References will be looking like this in Solution Explorer,

References

Step 4: Use the Below Method to your code for establishing CRM Connection, which consists of 3 parameters

Parameter Name Usage
UserName Online: Provide office 365 userid
On-Premise: Provide domain\username
Password Provide password
SoapOrgServiceUri Open Mscrm online or on-premise,
Goto Microsoft Dynamics CRM -> Settings ->
Customizations -> Developer Resources.
Under Service Endpoints -> Organization Service.
Ex: https://<OrgName>.api.crm5.dynamics.com/
XRMServices/2011/ Organization.svc
static IOrganizationService _service;

public static void ConnectToMSCRM(string UserName,string Password,string SoapOrgServiceUri)
{
    try
    {
        ClientCredentials credentials = new ClientCredentials();
        credentials.UserName.UserName = UserName;
        credentials.UserName.Password = Password;
        Uri serviceUri = new Uri(SoapOrgServiceUri);
        OrganizationServiceProxy proxy = new OrganizationServiceProxy(serviceUri, null, credentials, null);
        proxy.EnableProxyTypes();
        _service = (IOrganizationService)proxy;
    }
    catch (Exception ex)
    {
        Console.WriteLine("Error while connecting to CRM " + ex.Message);
        Console.ReadKey();
    }
}

Step 5: For this example, I am using Console application. Finally the code looks like this,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using System.ServiceModel.Description;
namespace ConnectToCRM
{
   class Program
   {
       static IOrganizationService _service;
        static void Main(string[] args)
       {
           ConnectToMSCRM("arunpotti@XYZORG.onmicrosoft.com", "XYZORGPassword", "https://XYZORG.api.crm5.dynamics.com/XRMServices/2011/Organization.svc");
           Guid userid = ((WhoAmIResponse)_service.Execute(new WhoAmIRequest())).UserId;
           if (userid != Guid.Empty)
           {
               Console.WriteLine("Connection Established Successfully");
               Console.ReadKey();
           }
       }
       public static void ConnectToMSCRM(string UserName,string Password,string SoapOrgServiceUri)
       {
           try
           {
               ClientCredentials credentials = new ClientCredentials();
               credentials.UserName.UserName = UserName;
                credentials.UserName.Password = Password;
               Uri serviceUri = new Uri(SoapOrgServiceUri);
               OrganizationServiceProxy proxy = new OrganizationServiceProxy(serviceUri, null, credentials, null);
               proxy.EnableProxyTypes();
               _service = (IOrganizationService)proxy;
           }
           catch (Exception ex)
           {
               Console.WriteLine("Error while connecting to CRM " + ex.Message);
               Console.ReadKey();
           }
       }
   }
}

Step 6: Build the project and Click on Start, you can see the below output

Connect To CRM Output

24 thoughts on “Connect to CRM Online or On-premise using C# SDK

  1. I think this is one of the most vitql info for me. And i am
    glad reading your article. But wamna remark on some general
    things, The website style is ideal, the articles is reeally excellent
    : D. Good job, cheers

  2. Hi Arun,
    Thanks for this explainatory post.
    I am trying this but in WebResource ,it is showing the type or namespace name WebResource coulod not be found( are You missing using directive or namespace reference)

  3. Hi there! I could have sworn I’ve visited this blog before but after browsing through a few of the posts I realized it’s new to
    me. Nonetheless, I’m definitely delighted I
    came across it and I’ll be bookmarking it and checking back frequently!

  4. We’re a group of volunteers and opening a new scheme in our community.
    Your site offered us with valuable information to work on. You’ve done an impressive
    job and our entire community will be grateful
    to you.

  5. Arun,
    Thank you for this amazing article. For a Noob CRM developer, the MS Documentation and SDK examples are completely unfathomable! I couldn’t get the SDK sample programs to connect to my CRM org, and deciphering the thousands of lines of code was impossible. With your clear description of how to set up the references and assemblies, I got connected to CRM in five minutes 🙂
    Thanks,
    Tony

Leave a Reply