1

I'm looking for some simple boilerplate code I can use to run a query against the Dynamics 365 API and grab out some JSON.

Preferably using WebClient or HttpClient. Nothing fancy. Simplest, reusable example gets the answer.

Mark Micallef
  • 2,643
  • 7
  • 28
  • 36

1 Answers1

1

You can find example code in SDK samples. Same is explained here.

Some key points:

1.Read the comments inside the code. Very important one:

/// Before building this application, you must first modify the following configuration   
/// information in the app.config file:  
///   - All deployments: Provide connection string service URL's for your organization.  
///   - CRM (online): Replace the application settings with the correct values for your    
///                 Azure app registration.

2.The method ConnectToCRM will do authentication piece & HttpClient call

3.Almost every single type of query including fetchxml is explained in the code sample

If you need help to get AccessToken from Azure registered CRM appId, then refer Jason Lattimer blog.

The overall simple boiler plate code & steps can be found in Inogic blog.

HttpClient httpClient= null;
httpClient = new HttpClient();
 //Default Request Headers needed to be added in the HttpClient Object
httpClient.DefaultRequestHeaders.Add("OData-MaxVersion", "4.0");
httpClient.DefaultRequestHeaders.Add("OData-Version", "4.0");
httpClient.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
 
 //Set the Authorization header with the Access Token received specifying the Credentials
 httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _result.AccessToken);