Console Application to Connect to Microsoft Dynamics 365 Customer Engagement (CRM) 9.x version

In Microsoft Dynamics 365 Customer Engagement(CRM) console applications are very useful in many scenarios.

In this post, we have will learn how to connect a .Net console application to Dynamics 365 Customer Engagement(CRM) online 9.x version (Latest D365 Customer Engagement(CRM)) and we’ll also implement one scenario.

Below are some use cases of console applications in Dynamics Customer Engagement(CRM):

  •  For quick testing some logic in any instance (dev/uat/production)
  •  Testing/debugging plugin code in live Customer Engagement(CRM) instance without interrupting live Customer Engagement(CRM) instance operations.
  •  Building a schedule to perform some specific task regularly. eg: Sending birthday email daily at midnight to the people having birthday today
  • Testing some third party API like API for SMS integration
  • Data migration from different systems like SQL database or from the older version of Customer Engagement (Customer Engagement(CRM)) to your system

Some pros of using Console applications in Dynamics Customer Engagement:

  • Easy to maintain the code
  • The visible output on screen or in a file as per requirement
  • Easy to connect to any Customer Engagementinstance, just paste credential and get started (if you keep connection code ready)
  • Very useful in testing the plugin logic on multiple environments without actually deploying it,just change credentials and play

There can be n number of a scenario where we can use console applications. What’s more important is, are you able to cast your logic into .Net code and run it in a console application.

Now we’ll discuss below scenario and ill implement it using a.Net console Application

Scenario:

Building a console application which connects to the Dynamics Customer Engagement 9.x version and finds out the count of contacts in the Customer Engagement system.

Prerequisites:

  • Visual Studio
  • Microsoft.Net framework 5.2 or higher
  • A Dynamics Customer Engagement(CRM) instance 9.x version

Step 1:

Open Visual Studio and click on File>New>Project. Click on Visual C#>Console App(.Net Framework), set the project name and click OK.

Step 2:

Now go to Tools>NuGet Package Manager and click on Package Manager Console. A package manager console will get opened.

Step 3:

To add required dlls in the project, paste below code into package manager console and hit enter

Install-Package Microsoft.Customer Engagement(CRM)Sdk.CoreTools -Version 9.0.0.7

Step 4:

On successful installation, you’ll get a successful installation confirmation message as below :

Make sure you can see a folder named core tools in the bin in solution explorer as shown below:

Step 5:

Right click on References, Click Browse And add following references to the project:

Microsoft.Customer Engagement(CRM).Sdk.Proxy.dll
Microsoft.Xrm.Sdk.dll

Again add the following reference into the project:

System.Runtime.Serialization
System.ServiceModel

Step 6:

Make sure you can see all the added dlls in references as below:

Step 7:

Add the following namespaces into the project:

using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Customer Engagement(CRM).Sdk.Messages;
using System.Net;
using System.ServiceModel.Description;

Step 8:

Add the following code below your main function:

public static IOrganizationService ConnecttoCustomer Engagement(CRM)()
 {
 IOrganizationService organizationService = null;

String username = "<paste-username-here>";//eg: [email protected]
 String password = "<paste-password-here>";//eg: [email protected]

// Get the URL from CRM, Navigate to Settings -> Customizations -> Developer Resources
 // Copy and Paste Organization Service Endpoint Address URL
 String url = "<paste-organisationservice-url-here>"; //eg: https://<yourorganisationname>.api.crm8.dynamics.com/XRMServices/2011/Organization.svc
 try
 {
  ClientCredentials clientCredentials = new ClientCredentials();
  clientCredentials.UserName.UserName = username;
  clientCredentials.UserName.Password = password;

 // For Dynamics 365 Customer Engagement V9.X, set Security Protocol as TLS12
  ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

 organizationService = (IOrganizationService)new OrganizationServiceProxy(new Uri(url),null, clientCredentials, null);

  if (organizationService != null)
   {
     Guid gOrgId = ((WhoAmIResponse)organizationService.Execute(new WhoAmIRequest())).OrganizationId;
     if (gOrgId != Guid.Empty)
      {
       Console.WriteLine("Connection Established Successfully...");
      }
   }
   else
   {
    Console.WriteLine("Failed to Established Connection!!!");
   }
 }
 catch (Exception ex)
 {
 Console.WriteLine("Exception occured - " + ex.Message);
 }
 return organizationService;

}

Also, add following code into your main function:

IOrganizationService service = ConnecttoCRM();
 if (service != null)
 {
    Console.ReadLine();
    //code your logic here
 }

After copy paste of the above code, your code will look like below:

Step 9:

To test the Customer Engagement connection, Click on start

On successful connection, your console should show the message as below:

Step 10:

Now to run the application for our scenario, first, add below namespace to your project:

using Microsoft.Xrm.Sdk.Query;

Also, add following code to your main function as shown below:

 //Get count of contacts in the system
 QueryExpression qe = new QueryExpression("contact");
 EntityCollection enColl = service.RetrieveMultiple(qe);
 Console.WriteLine("Total Contacts in the System=" + enColl.Entities.Count);

Step 11:

Click start button to run the application, you’ll get the result as below:

Issue:

If you face any issue in building the project, right click on project, properties to make sure the .Net framework in 4.5.2 or above.

So this is how you build a simple console application and connect it with Customer Engagement

Test your plugin code using console application, just paste it in the main function and debug the application.



Leave a Reply

This website uses cookies to enhance your browsing experience.