Skip to main content

Notifications

Dynamics 365 Community / Blogs / Dynamix Academy / How to Connect to Dynamics ...

How to Connect to Dynamics 365 using C# Console Application?

Article on How to connect to dynamics 365 from c#? In this article, we have explained how to connect to Dynamics CRM 365 online using C# example with a demo code as well?

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

Pre-Requisites before you start writing code for connect to dynamics 365 from c#

Must have

  • Basic Functional understanding of Dynamics 365.
  • Basic programming knowledge on .NET with C# or any other Object-Oriented Programming language.
  • Visual Studio Community Edition 2015/2017 or Higher Installed.

Good to have

Step by Step: Connecting to Dynamics 365 using a C# Console App

  • 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.
  • Step 2: Go to Visual Studio Tools -> NuGet Package Manager and click on 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
  • Step 4: Latest CRM SDK DLLs are installed in the project’s bin\coretools folder. You can also see the success message in the Package Manager Console.
  • Step 5: Right Click on References and navigate to the bin\coretools folder. Add the below Dlls to the Visual Studio project.
    • Microsoft.Crm.Sdk.Proxy.dll
    • Microsoft.Xrm.Sdk.dll
    • System.Runtime.Serialization
    • System.ServiceModel
  • 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;
  • Step 7: Copy and Paste the below code in the 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 Successful!...");
 }
 }
 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 the build is succeeded, Run the application to see the below Message.
c# in dynamics 365
C# in dynamics 365

Also, find More on Dynamix Academy :

You can also watch our Videos on playlist:

Let’s Connect:

The post How to Connect to Dynamics 365 using C# Console Application? appeared first on Dynamix Academy.

Comments

*This post is locked for comments