Connecting to Dynamics CRM from Console Application – Part II

Connecting to Dynamics CRM from Console Application – Part II

In a recent article I explained how to establish a connection from a Console application to Microsoft Dynamics CRM On-Premise. In this post, I will cover the same steps on how to connect a Console application to Microsoft Dynamics CRM/365 Online using the Microsoft.Xrm.Sdk and Microsoft.Xrm.Tooling.Connector namespaces

The first thing that we need is to download the Microsoft Dynamics SDK from the Microsoft web site. Based on your version of CRM (or Dynamics 365), this url, might be different, but you can download the SDK from here. After you download and install the SDK, you will need to add references to the Microsoft.Xrm.Sdk and Microsoft.Xrm.Tooling.Connector assemblies to your Visual Studio Console application. These can be found in the SDKbin directory.

Once the references have been added, add the following lines of code to your namespace declaration in the Program.cs code window:

using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Tooling.Connector;

When using the Xrm.Tooling.Connector namespace, we connect to Microsoft Dynamics CRM using the CrmServiceClient, but if you still require or want to use the OrganizationService you can use that after we establish connectivity to Dynamics CRM using the CrmServiceClient. We are going to show how to use the CrmServiceClient to connect using both a connection string, and individual settings.

To connect using a Connection String we will store the connection string in the App.Config file. The connection string will look as such:

  <connectionStrings>
    <add name="Office365"
         connectionString="Url=https://contoso.crm.dynamics.com; Username=crmadmin@contoso.onmicrosoft.com; Password=abcdef1234; authtype=Office365"/>
  </connectionStrings>

After we added the connection string to the App.Config file, we need to add code to our Main method that will establish the connection using the supplied string. We will perform this logic as follows:

string ConnectionString = ConfigurationManager.ConnectionStrings["Office365"].ConnectionString;
CrmServiceClient conn = new Microsoft.Xrm.Tooling.Connector.CrmServiceClient(connectionString);

if (conn.IsReady)
{
   // Perform Additional Actions Here
}

Another way to connect to Microsoft Dynamics CRM using CrmService Client is by providing parameters for credentials, organization url and organization name. The following is the Application Settings section within the App.Config file which we used to store the credentials and connection information. Note that password is not encrypted in the App Settings, but it should be encrypted one way or another. The following shows the AppSettings section in the App.Config file:

  <appSettings>
    <add key="UserName" value="crmadmin@contoso.onmicrosoft.com"/>
    <add key="Password" value="abcdef1234" />
    <add key="InternalUrl" value="contoso.crm.dynamics.com"/>
    <add key="OrgName" value="contosocrm"/>
    <add key="Region" value="NorthAmerica"/>
  </appSettings>

We can then read the application settings from our Program.cs Main function and establish a connection the same way we did with the connection string as follows:

string userName = ConfigurationManager.AppSettings["UserName"].ToString();
string password = ConfigurationManager.AppSettings["Password"].ToString();
string internalUrl = ConfigurationManager.AppSettings["InternalUrl"].ToString();
string orgName = ConfigurationManager.AppSettings["OrgName"].ToString();
string region = ConfigurationManager.AppSettings["Region"].ToString();

// Notice Authentication Type below is set to Office 365
NetworkCredential creds = new NetworkCredential(userName, ConvertToSecureString(password));
Microsoft.Xrm.Tooling.Connector.AuthenticationType authType = Microsoft.Xrm.Tooling.Connector.AuthenticationType.Office365;
CrmServiceClient conn = new Microsoft.Xrm.Tooling.Connector.CrmServiceClient(userName, ConvertToSecureString(password), region, orgName, isOffice365: true);

if (conn.IsReady)
{
   // Perform Additional Actions Here
}

As mentioned in the beginning, we are now able to use the CRMServiceClient class to perform actions against CRM. If we need to use the OrganizationService interface, we can use that as well, by adding the following line of code after setting the CrmServiceClient connection or after checking if the connection is ready as shown here:

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

At this point you can perform actions against the CRM Organization using either the Organization Service or Crm Service Client. In the next article, we will replicate the same example, but connect to Microsoft Dynamics CRM Online using the same methods.