top of page

Learning Center

  • Writer's pictureElegant Software Solutions

Example of Getting Sales Data from the D365 Rest API

Here is an example of how you can create a sample application using C# and Azure Functions to use REST APIs to read sales data from the D365 REST API:

  • First, create a new Azure Functions project in Visual Studio.

  • Install the following NuGet package: Microsoft.Net.Http

  • In the Azure Functions project, create a new function with an HTTP trigger. This function will be responsible for reading sales data from the D365 REST API.

  • In the function's code, add the following using statements:


using System;
using System.Net.Http;
using System.Threading.Tasks;
  • Next, you will need to provide the connection details for the D365 REST API. You can do this by adding the following code to your function:


string instanceUrl = "https://yourorg.crm.dynamics.com";
string accessToken = "youraccesstoken";

HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", accessToken);
  • Now, you can use the HttpClient object to make REST API calls to D365. For example, the following code retrieves all sales orders from D365:


string salesOrderUrl = $"{instanceUrl}/api/data/v9.1/salesorders?$select=name,totalamount,statuscode";
HttpResponseMessage response = await client.GetAsync(salesOrderUrl);
response.EnsureSuccessStatusCode();

string responseContent = await response.Content.ReadAsStringAsync();
var salesOrders = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(responseContent);
  • Finally, you can return the sales order data to the caller by adding the following code to your function:


return req.CreateResponse(HttpStatusCode.OK, salesOrders);

This is just a basic example of how you can use Azure Functions and the D365 REST API to read sales data from D365. You can modify the REST API query and add additional logic to the function as needed to meet the requirements of your application.

131 views0 comments

Recent Posts

See All

Comments


bottom of page