Microsoft has downloadable samples that demonstrate using the Dynamics 365 WebAPI with C#. The samples contain examples of using HttpClient getasync, sendasync with JSON.
The samples can be downloaded at https://code.msdn.microsoft.com/CRM-Web-API-Functions-and-a44416bb, with the code walkthrough at https://docs.microsoft.com/en-us/dynamics365/customer-engagement/developer/webapi/web-api-functions-actions-sample-csharp.
In this post we will configure the code to run with Dynamics 365.
First, click to download:
You will see:
Open the solution in Visual Studio:
You will see:
In NuGet, click Restore in the top right:
Open the app.config file. In this post we will be running this code on Dynamics 365 (i.e. CRM Online). Rename the “default” key to “onprem, and the “CRMOnline” key to “default”. Enter your D365 credentials. Also, register an app on Azure and copy the Application Id into the Client Id field in the App.Config:
Note if you see the messages:
“The request body must contain the following parameter: ‘client_secret or client_assertion” or “The user or administrator has not consented to use the application with ID ‘*****’ named ‘*****’. Send an interactive authorization request for this user and resource”, ensure you are using the application id as the client id, and ensure delegated permissions in your Azure app as well as clicked Grant Permissions.
Your App.Config file should look like:
Build the solution and run. You will see output:
And records created in Dynamics 365.