CRM Crate

Connecting to Dynamics 365 CRM using a Console Application

In this course, we will learn to connect the Dynamics 365 CRM using a Console Application. Before we start, make sure to subscribe to CRM Crate so that you stay up-to-date in field of Microsoft Dynamics 365 CRM.

Why should we use a Console Application to connect with Dynamics 365 CRM?

Below are few practical scenarios for which we can use a console application to connect with Dynamics 365 CRM –

  • Perform the complex and huge data migration.
  • Provide client a UI or CMD based windows application to run the process as per the need.

Pre-requisite for building a Console Application

  • Visual Studio 2015 / 2017/ 2019
  • CRM SDK Nugget Packages

Create a new Console Applicaton Project

  • Open the Visual Studio >> Create a new project as shown below.

  • Select “Console App (.NET Framework)”. Name the new console application and click on Create.

Install the neccessary Nugget Packages

  • Right click on the newly created Project >> Select “Manage NuGet Packages”.

  • Install the below given nugget packages –

  1. Microsoft.CrmSdk.CoreAssemblies
  2. Microsoft.CrmSdk.Deployment
  3. Microsoft.CrmSdk.Workflow
  4. Microsoft.CrmSdk.XrmTooling.CoreAssembly
  5. Microsoft.IdentityModel.Clients.ActiveDirectory
  6. System.Configuration.ConfigurationManager

Connect to CRM using the C# console application code

  • We will need three parameters for connecting our console application to Microsoft Dynamics 365 CRM. Store the below given three parameters in your App.Config file.

  1. CRM Web URL.
  2. CRM User Login ID.
  3. CRM User Password.

<?xml version="1.0" encoding="utf-8"?>
    <add key="UserKey" value="YOUR USER ID"/>
    <add key="UserPassword" value="YOUR USER PASSWORD"/>
    <add key="Environment" value=""/>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2"/>

Use the below given code for Connecting the Dynamics CRM.

using System;
using Microsoft.Xrm.Tooling.Connector;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using System.Diagnostics;

namespace CRM_Crate_Console_Application
    class Execution
        static void Main(string[] args)
            Console.WriteLine("Console Application Started!");

                //Step 1 - Retrieving CRM Essential Information.
                string sEnvironment = System.Configuration.ConfigurationManager.AppSettings["Environment"].ToString();
                string sUserKey = System.Configuration.ConfigurationManager.AppSettings["UserKey"].ToString();
                string sUserPassword = System.Configuration.ConfigurationManager.AppSettings["UserPassword"].ToString();

                //Step 2- Creating A Connection String.
                string conn = $@" Url = {sEnvironment};AuthType = OAuth;UserName = {sUserKey}; Password = {sUserPassword};AppId = 51f81489-12ee-4a9e-aaae-a2591f45987d;RedirectUri = app://58145B91-0C36-4500-8554-080854F2AC97;LoginPrompt=Auto; RequireNewInstance = True";

                Console.WriteLine("Operating Environment : " + sEnvironment);

                //Step 3 - Obtaining CRM Service.
                using (var service = new CrmServiceClient(conn))

                    if (service != null)
                        //Executing YOUR CRM OPERATIONS.
            catch (Exception ex)
                Console.WriteLine("Error Occured : " + ex.Message);

  • Code Logic Explaination –

A. Step 1 = Retrieve the three essential parameters (URL, User ID and Password) from the App.Config.

B. Step 2 = Create a string connection made up of CRM URL, User ID, Password, App ID and Redirect URI.

C. Step 3 = Utilize the class “CrmServiceClient” for obtaining the CRM service. Pass the above created connection string to CrmServiceClient and recieve the CRM Service in the return.

Once the service is obtained, you can perform any operations within the CRM as per your needs.

Thus we learned how to connect the Console Application with Dynamics 365 CRM.

CRM Crate

All In One Platform For Learning Microsoft CRM.

4.2 5 votes
Article Rating
Notify of
Newest Most Voted
Inline Feedbacks
View all comments
3 years ago


from where did you get AppId and app identification?

Prashant Tirlotkar
3 years ago
Reply to  Necdet

Hi Necdet,

The AppId is static. You too can use the same value.

3 years ago

Hi sir

Thank you for sharing the knowledge could you please suggest from where we need to get the following

RedirectUri = app://7815837b-17c1-eb11-8235-000d3a538fe0

Thank u

Prashant Tirlotkar
3 years ago
Reply to  Govardhan

Hi Govardhan,

Thanks. You will have to use the same RedirectUri and AppId provided in the above code as per the Microsoft docs.

2 years ago

Hello Prashant,

Thank you for this post, would you please create a series on how to create an ASP.NET Core WebApi App that connects to CRM.


John V
2 years ago

Hi. Two things about this:

  1. The assembly “Microsoft.IdentityModel.Clients.ActiveDirectory” is deprecated. Up to you if you want to update your example page, but this is no longer correct.
  2. Regarding Necdet’s question, the AppId is not static and you should NOT copy someone else’s App ID. The App ID comes from an Active Directory registration that you need to create for your app, assuming you will be using it in ongoing fashion. If you’re just writing a one-off that will be used once or twice and then never used, then fine copy this AppID.
Kuldeep Rajpurohit
11 months ago

this is not executing by app password of pwa account why?

error: CRM Crate Security Engine - Disabled Right Click & Selection!


Well Done,
Welcome to CRM Crate

Stay tuned with us and get all latest updates and learning in Microsoft CRM and related techonologes.