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 9.0.0.7

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.

Microsoft.Crm.Sdk.Proxy.dll

Microsoft.Xrm.Sdk.dll

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

And also add the below and click on OK,

System.Runtime.Serialization

System.ServiceModel

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;

try
 {
 ClientCredentials clientCredentials = new ClientCredentials();
 clientCredentials.UserName.UserName = "<ProvideUserName>@<ProvideYourOrgName>.onmicrosoft.com";
 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>.dynamics.com/XRMServices/2011/Organization.svc"),
 null, clientCredentials, null);

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

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

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

Note:

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 :):):)

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

  1. Hi I am facing the issue “Exception caught – An unsecured or incorrectly secured fault was received from the other party. See the inner FaultException for the fault code and detail.”..Can please suggeest for it

  2. Awesome article. I just started learning C# and had no idea how to use Visual Studios. I spent close to a week trying to understand the VS, packages, References, namespaces, etc. and then a colleague sent this article over. Was able to connect in 30 minutes and understand exactly what you’re explaining.

    Thank you!

  3. Hi Arun,
    Thanks for this code and it worked really well.i didnot used the latest SDK Dll and i have used v8.0.2.1
    and it worked for me very well.

Leave a Reply