Use XRM tooling to execute a web request against web API
Note
Unsure about entity vs. table? See Developers: Understand terminology in Microsoft Dataverse.
The CrmServiceClient class object is used to perform actions on your Dynamics 365 data such as create, update, retrieve or delete data.
You can now use the CrmServiceClient.ExecuteCrmWebRequest method to execute a web request against XRM web API.
The following code sample demonstrates how you can execute a web request using ExecuteCrmWebRequest method.
Note
This method is only applicable when the authentication type is specified as OAuth or Certificate.
Create a record
The following code sample demonstrates how to create a record using the ExecuteCrmWebRequest method. In this example, you will create an account, and then display the ID in the response object.
Dictionary<string, List<string>> ODataHeaders = new Dictionary<string, List<string>>() {
{ "Accept", new List<string>() { "application/json" } },
{"OData-MaxVersion", new List<string>(){"4.0"}},
{"OData-Version", new List<string>(){"4.0"}}
};
using (CrmServiceClient svc = new CrmServiceClient(conn))
{
if (svc.IsReady)
{
HttpResponseMessage response = svc.ExecuteCrmWebRequest(HttpMethod.Get, "accounts?$select=name", "{ \"name\":\"Test Account\"}", ODataHeaders, "application/json");
if (response.IsSuccessStatusCode)
{
var accountUri = response.Headers.GetValues("OData-EntityId").FirstOrDefault();
Console.WriteLine("Account URI: {0}", accountUri);
}
else
{
Console.WriteLine(response.ReasonPhrase);
}
}
else
{
Console.WriteLine(svc.LastCrmError);
}
}
To learn more about using Dataverse Web API requests and responses, and handling errors, see Use the Microsoft Dataverse Web API.