How to quickly connect to Dynamics CRM/365 (online-onprem) using C#?



Note: for this walkthrough I’ve been using ConsoleApplication


Step 1: Add following .Net references:
        System.Runtime.Serialization
      System.ServiceModel

Step 2: Add following references available in Bin folder of the SDK
        Microsoft.Xrm.Sdk
      Microsoft.Crm.Sdk.Portal
Step 3: Add following namespaces in your class file 
      using System.Runtime.Serialization;
      using System.ServiceModel;
      using System.ServiceModel.Description;
      using Microsoft.Xrm.Sdk;
      using Microsoft.Xrm.Sdk.Client;
      using Microsoft.Xrm.Sdk.Query;
      using Microsoft.Xrm.Sdk.Discovery;
      using Microsoft.Crm.Sdk.Messages;

Step 4: Create a method EstablishConnetionWithCRM with following three parameters:
       UserName - Provide O365 Userid in case of online CRM, for on-prem proivde doamin\username
       Password - Provide Password
      ServiceUrl - Provide Organization Service URL by going through following path in CRM
Microsoft Dynamics CRM > Settings > Customization > Developer Resources > Service Endpoints > Organization Service

Step 5: Your EstablishConnetionWithCRM method should look like following:
Public static void EstablishConnetionWithCRM(string UserName, string Password, string ServiceUrl){
try{
                ClientCredentials credentials = new ClientCredentials();
                credentials.UserName.UserName = UserName;
                credentials.UserName.Password = Password;
                Uri serviceUri = new Uri(ServiceUrl);
                OrganizationServiceProxy sp = new OrganizationServiceProxy(serviceUri, null, credentials, null);
                sp.EnableProxyTypes();
                _service = (IOrganizationService)sp;

            }
            catch (Exception ex)
            {

                Console.WriteLine("Error: " + ex.Message);
            }
}

Step 6: Call EstablishConnetionWithCRM into the Main method and check if you are connected or not:
static void Main(string[] args)
        {
            EstablishConnetionWithCRM ("<USERNAME>", "<PASSWORD>", "https://<DOMAIN>/XRMServices/2011/Organization.svc");
            Guid userId = ((WhoAmIResponse)_service.Execute(new WhoAmIRequest())).UserId;
            if (userId != Guid.Empty)
                Console.WriteLine("Connection Established Successfully");
              Else
                Console.WriteLine("Connection was not Established");

            Console.ReadLine();
        }


Final code will be following

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Description;

using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Discovery;
using Microsoft.Crm.Sdk.Messages;

namespace connecToCRM
{
    class Program
    {
        static IOrganizationService _service;
        static void Main(string[] args)
        {
            EstablishConnetionWithCRM ("<USERNAME>", "<PASSWORD>", "https://<DOMAIN>/XRMServices/2011/Organization.svc");
            Guid userId = ((WhoAmIResponse)_service.Execute(new WhoAmIRequest())).UserId;
            if (userId != Guid.Empty)
                Console.WriteLine("Connection Established Successfully");
              Else
                Console.WriteLine("Connection was not Established");

            Console.ReadLine();
        } //End of Main

        Public static void EstablishConnetionWithCRM(string UserName, string Password, string ServiceUrl){
try{
                ClientCredentials credentials = new ClientCredentials();
                credentials.UserName.UserName = UserName;
                credentials.UserName.Password = Password;
                Uri serviceUri = new Uri(ServiceUrl);
                OrganizationServiceProxy sp = new OrganizationServiceProxy(serviceUri, null, credentials, null);
                sp.EnableProxyTypes();
                _service = (IOrganizationService)sp;

            }
            catch (Exception ex)
            {

                Console.WriteLine("Error: " + ex.Message);
            }
}//End of EstablishConnetionWithCRM function
    } //End of Class
}//End of namespace

~~YOU ARE DONE, ENJOY~~



Comments

Popular posts from this blog

How to hide '+' on a subgrid in dynamics CRM?

Pick and Removing Can't be processed at the same time. To pick this queue item, please unselect the option for removal.

Get Rid off Pending Email Warning - Microsoft Dynamics CRM