Step by Step: Connecting to Dynamics 365 using a C# Console App


If you’re new to Microsoft Dynamics 365 development you may be wondering how to connect to it via a .Net C# console application or would like to set up a Visual Studio project to get started with the Dynamics 365 SDK.

In this post I’ll take you through setting up the Visual Studio project, adding the Dynamics 365 SDK references, connecting to Dynamics 365 and running a basic query to validate.

Setting up a new Visual Studio 2017 Project with Dynamics 365 SDK References

  1. Open Visual Studio 2017 and select File | New | Project
  2. Click on Visual C# and select Console App (.Net Framework)
  3. Enter a project name within the Name field and click Ok

Dynamics-365-console-app-project

The next step is to add the Dynamics 365 SDK references to the project and this is quite easy now it’s managed via NuGet.

  1. Select Tools | NuGet Package Manager | Manage NuGet Packages for Solution
  2. This screen defaults to showing the installed packages, click Browse
  3. In the Search textbox type: Dynamics 365 XRMTooling and select Microsoft.CrmSdk.XrmTooling.CoreAssembly

Nuget-select-dynamics-365-xrmTooling

  1. Select the Project in the right-hand side window and click Install

Nuget-select-dynamics-365-core-XRMtools-step-2

  1. If prompted by a “Preview Changes” dialog box, click Ok
  2. Click I Accept when prompted by the License Acceptance dialog
  3. We need to add one more package,  search for Microsoft.CrmSdk.CoreAssemblies under the browse tab and follow the same process to add it to the project too.

Alternatively, this same process can be done from the Nuget command line by:

  • Navigate to Tools | Nuget Package Manager | Package Manager Console
  • Within the package manager console copy and paste the following command:

Note that as of this post, the latest version is 9.0.2.4, to get the latest version check here.

The process above has added all the SDK DLL’s we need within the project References, so there’s no messing around to manually add them.

Nuget-select-dynamics-365-SDK-coreassemblies

Now that’s done, we are ready to start coding!

Cutting the Code

  1. Add the following namespaces to the top of Program.cs:

Dynamics-sdk-namespaces-c-sharp-code

  1. Next comes the connection code and the aim is to create an IOrganizationService object which connects to Dynamics365 via a client connection and provides pragmatic access to Dynamics 365. Copy and paste the following code into the Main method:

Within the CrmServiceClient string replace the following variables in your code:

  • <USERNAME> = the Dynamics username e.g. someone@DYN365Demo.onmicrosoft.com
  • <PASSWORD> = the user password
  • <URL> = the Dynamics 365 instance URL. This is this is the same URL that would be used to access it from a web browser (minus  the page “/main.aspx#####”) e.g.:

https://DYN365Demo.crm6.dynamics.com

Dynamics-sdk-namespaces-c-sharp-code-setting-connection

  1. Add the following code underneath to check the connection by attempting to retrieve the current user ID:

And you’re done. Click the Start button to run the program and if the connection details are correct you should see “Connection Successful!”:

Visual-studio-output

To keep the example above simple I’ve left out proper error handling, but for the sake of completeness I’ve included the full code below with a try / catch and some additional outputs to the console to show where it’s at:

Feel free to leave any questions in the comments section below.

7 thoughts on “Step by Step: Connecting to Dynamics 365 using a C# Console App

      1. Hai Glen I also trying to connect d365 i getting the error “An unsecured or incorrectly secured fault was received from the other party. See the inner FaultException for the fault code and detail.”
        showing

        1. Hi Venkat,

          This error is usually caused by your computer’s clock being out of sync with the Dynamics server.

          I recommend checking the clock on both to make sure they are correct and try the connection again.

        2. May be if you are using latest CRM Online version then you may need to change Auth Type to OAuth as Office 365 is deprecated

Leave a Reply

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

Recent Posts