Saturday, September 10, 2016

Simple Dynamics CRM Console Application

This is a quick tip on how to create a simple console application in C# that connects to Dynamics CRM 2016 and creates an account record. There is some sample  code in the SDK that demonstrates how to do this but this sample is even simpler.

static void Main(string[] args)
    // Get the CRM connection string and connect to the CRM Organization
    CrmServiceClient crmConn = new CrmServiceClient(ConfigurationManager.ConnectionStrings["CRM"].ConnectionString);
    IOrganizationService crmService = crmConn.OrganizationServiceProxy;
    Entity acc = new Entity("account");
    acc["name"] = "Joe's New Account";

Firstly create a new console application in Visual Studio and then go to the NuGet Package Manager option and install the Microsoft Dynamics CRM 2016 SDK Xrm Tooling Package.

Edit the app.config file in your project and add a connectionsStrings section with your CRM connection string.

    <add name="CRM" connectionString="AuthType=Office365;Url=;; Password=MyPassword" />

Now add a reference to the System.Configuration assembly in your project so you can access your config file from code.

Add the following namespaces to your code

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

Now add the code shown at the top to your main method. When you run your console application it will connection to CRM and create an account record.