Simple Dynamics CRM Console Application

This is a quick tip on how to create a simple Dynamics CRM 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";
    crmService.Create(acc);
                
}

 

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.

Simple  Dynamics CRM Console Application Joe Gill Dynamics 365 Consultant


Edit the app.config file in your project and add a connectionsStrings section with your CRM connection string.
 
  <connectionStrings>
    <add name="CRM" connectionString="AuthType=Office365;Url=https://joegilltest.crm4.dynamics.com; Username=joe@joegilltest.onmicrosoft.com; Password=MyPassword" />
  </connectionStrings>
 
 
Now add a reference to the System.Configuration assembly in your project so you can access your config file from code.
 
Simple  Dynamics CRM Console Application Joe Gill Dynamics 365 Consultant
 

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 Dynamics CRM console application it will connection to CRM and create an account record.
 
 
Simple  Dynamics CRM Console Application Joe Gill Dynamics 365 Consultant
 
 
 

Leave a Reply