Connecting to Dynamics 365 from Console C# or Web API using CRMSERVICECLIENT

Recently I have seen lots of posts on CRM Community on how to establish a connection to CRM using Console Application or Web API. Here we will focus on using Office365 as an auth type for online instance.

One of the ways to connect through code to Dynamics 365 is to use CrmServiceClient, which allows us to connect using a connection string. CrmServiceClient is located in Microsoft.Xrm.Tooling.Connector.

Moving forward create a new Visual Studio project and add Microsoft.CrmSdk.XrmTooling.CoreAssembly  nuget package

Include below namespaces in class to use package.

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

Add a connection string to your project in below format

  <connectionStrings>
    <add name="Dev" connectionString="AuthType=Office365; Username=admin@sampleorg.onmicrosoft.com; Password=yourpassword; Url=https://sampleorg.crm.dynamics.com/; RequireNewInstance=true LoginPrompt=Never"/>
  </connectionStrings>

You can read more about connection string here: Use connection strings in XRM tooling to connect to Dynamics 365

Use the below code to create get IOrganizationService




Testing connection.. 



 Hope this helps you..!

Post a Comment

0 Comments