Step by Step to connect Dynamics 365 CRM Online V9.X using C# Console Application

Follow the below steps,

Step 1: Open Visual Studio and Click on New Project. Click on Visual C# and select Console Application.

Give Name to the Project and click on OK.

Connect to Dynamics 365 Online - Step1 Open Visual Studio

Step 2: Goto Visual Studio Tools -> NuGet Package Manager and click on Package Manger Console.

Connect to Dynamics 365 Online - Step2 Open Package Manager Console

Step 3: In Package Manager Console, Copy & Paste the below command to get the required CRM SDK Dlls and press Enter.

Click here to know the Latest version of the CRM Dlls.

Install-Package Microsoft.CrmSdk.CoreTools -Version

Connect to Dynamics 365 Online - Step3 Install CRM SDK Core Tools

Step 4: Latest CRM SDK Dlls are installed in the project’s bin\coretools folder. You can also see the success message in Package Manager Console.

Connect to Dynamics 365 Online - Step4 Add CRM SDK Core Tools to VS Project

Step 5: Right Click on References and navigate to bin\coretools folder.

Add the below Dlls to the Visual Studio project.



Connect to Dynamics 365 Online - Step5 Add CRM and Xrm SDK Dlls

And also add the below and click on OK,



Connect to Dynamics 365 Online - Step5 Add Runtime and Service Model References

Step 6: Add the below namespaces.

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

Connect to Dynamics 365 Online - Step6 Add Namespaces

Step 7: Copy and Paste the below Code in Main method.

IOrganizationService organizationService = null;

 ClientCredentials clientCredentials = new ClientCredentials();
 clientCredentials.UserName.UserName = "<ProvideUserName>@<ProvideYourOrgName>";
 clientCredentials.UserName.Password = "<ProvideYourPassword>";

// For Dynamics 365 Customer Engagement V9.X, set Security Protocol as TLS12
 ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
// Get the URL from CRM, Navigate to Settings -> Customizations -> Developer Resources
// Copy and Paste Organization Service Endpoint Address URL
organizationService = (IOrganizationService)new OrganizationServiceProxy(new Uri("https://<ProvideYourOrgName>.api.<CRMRegion>"),
 null, clientCredentials, null);

if (organizationService != null)
 Guid userid = ((WhoAmIResponse)organizationService.Execute(new WhoAmIRequest())).UserId;

if (userid != Guid.Empty)
 Console.WriteLine("Connection Established Successfully...");
 Console.WriteLine("Failed to Established Connection!!!");
 catch (Exception ex)
 Console.WriteLine("Exception caught - " + ex.Message); 

Step 8: Right click on the solution and build it. If build is succeeded, Run the application to see the below Message.

Connect to Dynamics 365 Online - Success Message


Problem: If you face any build issues like the below, while building the project, then use the below solution and resolve it.

Connect to Dynamics 365 Online - Step8 Build issues

Solution: Follow the below steps to resolve the problem.

  1. Change the Project’s Dot Net Framework from 4.5 to 4.5.2, this change is required because latest CRM SDK Dlls needs the 4.5.2 version of Dot Net Framework.

Connect to Dynamics 365 Online - .Net Framework 4.5.2

2. Click on Yes and build the project.

Connect to Dynamics 365 Online - Target Framework Change Message

For more info Click here.

Hope you have successfully connected to Dynamics CRM Online Version 9.X :):):)