
In this blog I will try to demonstrate further steps after connecting our dynamics CRM instance to Azure functions using webhooks. You can refer my first blog to get step by step guide on Azure functions with dynamics 365 here.Now we have the json request of CRM in httprequestmessage req Object as parameter as per the previous blog. The question arises how can we execute azure function under CRM context like we do in plugin?
Read JSON data from the Httprequestmessage :
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.
Parse JSON string to RemoteExecutionContext object:
Below code snippet deserialize the JSON string to RemoteExecutionContext object.
#r "bin/Newtonsoft.Json.dll"
#r "bin/Microsoft.Xrm.Sdk.dll"
#r "bin/System.Runtime.Serialization.dll"
using System.Net;
using System.Dynamic;
using System.Text;
public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
{
string jsonContext = await req.Content.ReadAsStringAsync();
log.Info("Read context: " + jsonContext);
jsonContext = FormatJson(jsonContext);
log.Info("Formatted JSON Context string: " + jsonContext);
Microsoft.Xrm.Sdk.RemoteExecutionContext remoteExecutionContext = DeserializeJsonString<Microsoft.Xrm.Sdk.RemoteExecutionContext>(jsonContext);
}
/// <summary>
/// Function to deserialize JSON string using DataContractJsonSerializer
/// </summary>
/// <typeparam name="RemoteContextType">RemoteContextType Generic Type</typeparam>
/// <param name="jsonString">string jsonString</param>
/// <returns>Generic RemoteContextType object</returns>
public static RemoteContextType DeserializeJsonString<RemoteContextType>(string jsonString)
{
//create an instance of generic type object
RemoteContextType obj = Activator.CreateInstance<RemoteContextType>();
MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(jsonString));
System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(obj.GetType());
obj = (RemoteContextType)serializer.ReadObject(ms);
ms.Close();
return obj;
}
In the above code snippet when we pass deserialised json string to the remocontexttype function it returns the remoteexecutioncontext of the plugin depending on which message webhook was registered.
Please Note: Add reference of following dlls,
- Xrm.Sdk.dll
- Runtime.Serialization.dll
Now we have the execution context we can read the values as we do for plugins.
//read Plugin Message Name
string messageName = remoteExecutionContext.MessageName;
//read execution depth of plugin
Int32 depth = remoteExecutionContext.Depth;
//read BusinessUnitId
Guid businessUnitid = remoteExecutionContext.BusinessUnitId;
//read Target Entity
Microsoft.Xrm.Sdk.Entity targetEntity = (Microsoft.Xrm.Sdk.Entity)remoteExecutionContext.InputParameters["Target"];
//read attribute from Target Entity
string phoneNumber = targetEntity.Attributes["telephone1"].ToString();
log.Info("Message Name: " + messageName);
log.Info("BusinessUnitId: " + businessUnitid);
log.Info("Plugin Depth: " + depth);
log.Info("TargetEntity Logical Name: " + targetEntity.LogicalName);
log.Info("Phone Number: " + phoneNumber);
The steps given above describes how to Parse JSON string that represents the Dynamics 365 plugin execution context received in Azure Function.Hope this helps and thank you for reading .All the best 🙂
