Connecting to Dynamics 365 using CrmServiceClient

1 Comment

One of the ways to connect through code to Dynamics 365 is to use the CrmServiceClient, which allows us to connect using a connection string. This class is located in Microsoft.Xrm.Tooling.Connector.

To use it, create a new Visual Studio project and add the dll Microsoft.Xrm.Tooling.Connector. Then add the using line:

using Microsoft.Xrm.Tooling.Connector;

You will also need to add Microsoft.Xrm.SDK dll and using statement:

using Microsoft.Xrm.Sdk;

We will add some code to simply connect to Dynamics CRM:

Note the connection string is in the format:

var connectionString = @”AuthType=Office365;Url=https://yourcrm.crm.dynamics.com/;Username=youremail;Password=yourpassword”;

You can read more about the options for the connection strings here: https://msdn.microsoft.com/en-us/library/mt608573.aspx

Let’s run the code in debug with a breakpoint. You can see the code executes successfully:

The conn object returns with data from CRM:

From here, we have the ability to perform operations using this connection. The first thing to do is to cast the connection as an IOrganizationService and create a CrmServiceClient:

CrmServiceClient conn = new CrmServiceClient(connectionString);
IOrganizationService _orgService = (IOrganizationService)conn.OrganizationWebProxyClient != null ? (IOrganizationService)conn.OrganizationWebProxyClient : (IOrganizationService)conn.OrganizationServiceProxy;

Now, we can run some code to get the CRM version number. To do this add the assembly Microsoft.Crm.Sdk.Proxy from the SDK bin. You may also need the assembly System.ServiceModel. You can then add the using statement:

using Microsoft.Crm.Sdk.Messages;

Code to get the version:

RetrieveVersionRequest versionRequest = new RetrieveVersionRequest();

RetrieveVersionResponse versionResponse =

(RetrieveVersionResponse)_orgService.Execute(versionRequest);

Console.WriteLine(“Microsoft Dynamics CRM version {0}.”, versionResponse.Version);

Console.ReadLine();

The code should now look like this:

When running the code, you can see the version number is returned:

Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
using Microsoft.Xrm.Tooling.Connector;
using Microsoft.Xrm.Sdk;
using Microsoft.Crm.Sdk.Messages;
 
namespace GetVersion
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                var connectionString = @"AuthType = Office365; Url = https://yourcrm.crm.dynamics.com/;Username=yourusername;Password=yourpassword";
CrmServiceClient conn = new CrmServiceClient(connectionString);
 
                IOrganizationService _orgService;
                _orgService = (IOrganizationService)conn.OrganizationWebProxyClient != null ? (IOrganizationService)conn.OrganizationWebProxyClient : (IOrganizationService)conn.OrganizationServiceProxy;
 
                RetrieveVersionRequest versionRequest = new RetrieveVersionRequest();
                RetrieveVersionResponse versionResponse = (RetrieveVersionResponse)_orgService.Execute(versionRequest);
 
                Console.WriteLine("Microsoft Dynamics CRM version {0}.", versionResponse.Version);
                Console.ReadLine();
 
            }
            catch (Exception ex)
            {
               // Implement error handling 
            }
        }
    }
}

 

THANKS FOR READING. BEFORE YOU LEAVE, I NEED YOUR HELP.
 

I AM SPENDING MORE TIME THESE DAYS CREATING YOUTUBE VIDEOS TO HELP PEOPLE LEARN THE MICROSOFT POWER PLATFORM.

IF YOU WOULD LIKE TO SEE HOW I BUILD APPS, OR FIND SOMETHING USEFUL READING MY BLOG, I WOULD REALLY APPRECIATE YOU SUBSCRIBING TO MY YOUTUBE CHANNEL.

THANK YOU, AND LET'S KEEP LEARNING TOGETHER.

CARL

https://www.youtube.com/carldesouza

 

ABOUT CARL DE SOUZA

Carl de Souza is a developer and architect focusing on Microsoft Dynamics 365, Power BI, Azure, and AI.

carldesouza.comLinkedIn Twitter | YouTube

 

One Response to Connecting to Dynamics 365 using CrmServiceClient

  1. The API for the CRM SDK is totally dodgy.
    CrmServiceClient conn = new CrmServiceClient(connectionString);
    As per anything across a network, the time to connect varies. is it 5 secs or 1 minute?
    I know we can check .IsReady property, but there should be an event or callback that fires once connected.

Leave a Reply

Your email address will not be published. Required fields are marked *