Connecting to Dynamics 365 using CrmServiceClient

Leave a 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=;Username=youremail;Password=yourpassword”;

You can read more about the options for the connection strings here:

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 =


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


The code should now look like this:

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


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)
                var connectionString = @"AuthType = Office365; Url =;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);
            catch (Exception ex)
               // Implement error handling 



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

carldesouza.comLinkedIn Twitter | YouTube


Leave a Reply

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