Parse JSON string that represents the Dynamics 365 plugin execution context received in Azure Function

By | June 25, 2018

Introduction:

In our previous blogs of this Integrating Dynamics 365 with Azure Functions series, we have gone through a walkthrough with an example of creating an Azure Function and call the same through the following,

  1. An example of directly calling an Azure function from traditional workflows.
  2. Register as a WebHook and invoke it from a workflow.
  3. Register as a WebHook and register steps for messages that you would like the custom logic to be executed for.

In this blog, we will illustrate how to parse the JSON data that we received in the Azure Function. Let’s consider that we have registered a plugin step on an update of Account record which invoke a WebHook (here in our case Azure Function).

  • Read JSON data from the request body:

When plugin triggers and invokes a WebHook, three types of data received in the request i.e. Query String, Header Data and Request Body. The Request body contains a string that represents the JSON value of the RemoteExecutionContext class. This class defines the contextual information sent to a remote service endpoint at run-time. Below code snippet reads the content from the HttpRequestMessage and converts the received JSON string to proper deserializable JSON string.

 

Remark: Add reference of System.Text so that we can use Regex expression.

 

  • Sample JSON string that gets received in the Azure Function:

Below is the sample JSON string.

  • Parse JSON string to ExpandoObject dynamic object:

Below code snippet deserialize the JSON string to dynamic ExpandoObject.

dynamic dynObj = Newtonsoft.Json.JsonConvert.DeserializeObject(jsonContext);

  • Read values from dynamic object:

Simple string type,

log.Info(“BusinessUnitId: ” + dynObj["BusinessUnitId"]);

or

log.Info(“BusinessUnitId: ” + dynObj.BusinessUnitId);

Complex ParameterCollection type,

log.Info(“InputParameters->Target->LogicalName: ” + dynObj["InputParameters"][0]["value"]["LogicalName"].ToString());

  • Parse JSON string to RemoteExecutionContext object:

Below code snippet deserialize the JSON string to RemoteExecutionContext object

 

Remarks: Add reference of following dlls,

  • Xrm.Sdk.dll
  • Runtime.Serialization.dll

Below code snippet shows how to read values,

 

Parse JSON

Conclusion:

The steps given above describes how to Parse JSON string that represents the Dynamics 365 plugin execution context received in Azure Function. To read all blogs about Azure Functions visit: http://bit.ly/inogic-azurefunctions

Copy System and Custom Entities in Dynamics CRM

Leave a Reply