Please wait, loading...

 

Connect to Dynamics CRM / D365 WebApi v9 from Console Application C#

January 4, 2018
There are 2 major steps involved in the process.
  • Register the application with your Azure subscription
  • Build the Console Application

Prerequisites

CRM / D365 Trial Organisation

Register the application with your Azure subscription


Step 1 Login to azure portal  https://portal.azure.com with your CRM credentials

Step 2 Click on Azure Active Directory – App registrations

 Step 3 Click on to New application registration

 Step 4 Enter Your Desired Name and Redirect URL but select Native in Application type and click on create Button

Step 5 After some some time you will be able to see your created apps then click on it

Step 6  Then click on Required permissions under API Access and then click on Add

Step 7  Click on Select API and then Select Dynamics CRM Online and Click on Select

Step 8  Select Access CRM Online as organisation users checkbox and click on Select Button

Step 9 Then click on Done

Build the Console Application 

Step 1 Navigate to Microsoft Url for sample for web API or click here quick start sample and extract to your desired location

Step 2 Open the Solution file in vs and you have to edit the code by reading the comment with TODO tag

Step 3 Update the service URLwith your current CRM organisation

Eg:

        private static string serviceUrl = "https://sa##x.crm8.dynamics.com";   // CRM Online

To get the client ID go to your app in Azure and copy the GUID specified for Application ID

To get the client ID go to your app in Azure and copy the GUID specified for Application ID and redirecting URL to Home Page URL ( will be same as you gave at the time of app creation in Redirecting URL)

Your Code should look like below

private static string serviceUrl = "https://sablecafex.crm8.dynamics.com";   // CRM Online
private static string userAccount = null;  //CRM user account
private static string domain = null;  //CRM server domain
private static string clientId = "e5cf0024-a66a-4f16-85ce-99ba97a24bb2";
private static string redirectUrl = "http://localhost";  //e.g. "http://localhost/SdkSample"

Step 3 Update Authentication Context with direct Authorisation Endpoint URL

To get Authorisation Endpoint URL go to your app in Azure click on Endpoints button

And copy the OAUTH 2.0 Authorization Endpoint URL

Replace the previous lines for getting authority information and update with the below code
string authority = "https://login.microsoftonline.com/e5cf0024-a66a-4f16-85ce-99ba97a24bb2/oauth2/authorize";
AuthenticationContext authContext = new AuthenticationContext(authority, false);

Your Code should look like below

//AuthenticationParameters ap = AuthenticationParameters.CreateFromResourceUrlAsync(
//        new Uri(serviceUrl + "api/data/")).Result;
//AuthenticationContext authContext = new AuthenticationContext(ap.Authority, false);

string authority = "https://login.microsoftonline.com/e5cf0024-a66a-4f16-85ce-99ba97a24bb2/oauth2/authorize";
AuthenticationContext authContext = new AuthenticationContext(authority, false);

AuthenticationResult authResult = authContext.AcquireToken(serviceUrl, clientId, new Uri(redirectUrl), PromptBehavior.Always);

The above code is connection with Prompt dialogue, for connection without prompt see my another thread – without prompt

Step 4 Rebuild

 Include the below reference to your project(to global config file the line before where your client credentials is configured) and change the .Net framework to 4.6.1 and rebuild 


“ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12”

for More details on it refer to my another thread here 

Step 5 Rebuild Your code and run it you will be prompted with sign in page enter your credentials

Step 5 Press on Accept

 And Your are done

You can have a look at my code here : My Sample Code

Inspired By : Nishant Rana’s Weblog

Happy Coding and CRMing 😊

SOURCE : mscrm.com

https://i0.wp.com/microsoftdynamics.in/wp-content/uploads/2020/04/Microsoftdynamics365.png?fit=640%2C651&ssl=1
Microsoft Dynamics Community Profile

Learn more