Passing data from Dynamics 365 to Azure Service Bus Queue using Plugins/Workflows

By | August 16, 2018

Introduction:

Recently we had a business requirement where we need to pass data from Dynamics 365 CRM to Azure Service Bus queue through plugins and workflows. After some research and play around we found a solution for this.

For this, we require an Azure Service Bus, an Azure function and a webhook. The CRM data can be delivered to azure portal using the webhook. The webhook is registered to connect to the azure function. When plugin/workflow is triggered, webhook is called from the code which passes the data to azure function in JSON format and azure function is used to add the data to the queue of Azure service bus (ASB).

Passing data from Dynamics 365 to Azure Service Bus Queue using Plugins Workflows

The detailed steps are as follows:-

1. Create an Azure Service Bus:-

Open Azure portal in your CRM organization and Create Service Bus Namespace by navigating to + Create a Resource >> Integration >> Service Bus

2. Create an Azure function which will add the CRM data to Azure Service Bus Queue:-

a. Navigate to + Create a Resource >> Compute >> Function App

b. Create a C# HttpTrigger Function in it

c. Click on “Get function URL” link

Passing data from Dynamics 365 to Azure Service Bus Queue using Plugins Workflows

Passing data from Dynamics 365 to Azure Service Bus Queue using Plugins Workflows

This URL link is important and will be used in CRM Plugin/Workflow code and webhook registration.

d. Click on Integrate and + New Output to create a queue in ASB and provide it to Azure function

assing data from Dynamics 365 to Azure Service Bus Queue using Plugins Workflows

e. Select Azure Service Bus

Passing data from Dynamics 365 to Azure Service Bus Queue using Plugins Workflows

f. Select “Service Bus Queue” in Message Type. You can select the “Service Bus Connection” by clicking on new and selecting the desired Service Bus.

Passing data from Dynamics 365 to Azure Service Bus Queue using Plugins Workflows

g. Click on the function “f functionname” and paste the following code to add the JSON data from CRM to the ASB Queue in the azure function. Save the code.

using System;
using System.Net;
public static async Task<object> Run(HttpRequestMessage req,IAsyncCollector<Object> outputSbMsg, TraceWriter log)
{
    log.Info($"Webhook was triggered!");
    string jsonContent = await req.Content.ReadAsStringAsync();
    log.Info("jsonContent " + jsonContent);
    await outputSbMsg.AddAsync(jsonContent);
    log.Info("added to queue");    
    return req.CreateResponse(HttpStatusCode.OK);
}

Here,     HttpRequestMessage req is  JSON data from CRM

IAsyncCollector<Object> outputSbMsg  is  ASB Queue name

Passing data from Dynamics 365 to Azure Service Bus Queue using Plugins Workflows

3. Assume we are sending the data of Account entity to Azure Service Bus (ASB). The data should be in JSON format before it is passed to Azure portal. Below is the plugin/workflow code:-

// Create a Class having members as data attributes to be passed to Azure
[DataContract]
public class AccountObj
{
[DataMember]
public string AccountID { get; set; }
[DataMember]
public string Name { get; set; }
[DataMember]
public string Telephone { get; set; }
}
// Insert the following code after getting the primary entity in Plugin/workflow
using System.IO;
using System.Net;
using System.Runtime.Serialization.Json;//Install from NuGet Package Manager

using (WebClient client = new WebClient())
{
// Prepare Json data                       	
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(AccountObj));
MemoryStream memoryStream = new MemoryStream();
serializer.WriteObject(memoryStream, accObj);
var jsonObject = Encoding.Default.GetString(memoryStream.ToArray());

// Prepare WebHook
var webClient = new WebClient();
webClient.Headers[HttpRequestHeader.ContentType] = "application/json";

// Azure Function key
var code = "xPhPPB5tGCq86NRbe7wzgJika3bv4ahP9kw7xe5Asoja2vEk4fPqVw==&clientId=default";
// Azure Function Url
var serviceUrl = "https://callwebhookfromcrmassebly.azurewebsites.net/api/GenericWebhookCSharp1?code=" + code;

// upload the json data to the serviceurl 
string response = webClient.UploadString(serviceUrl, jsonObject);
  }

4. Webhook Registration:-

With July 2017 Update, now we have the option to register a new Webhook through Plugin Registration tool. Download the latest Plugin Registration Tool from NuGet using the PowerShell script 

https://docs.microsoft.com/en-us/dynamics365/customer-engagement/developer/download-tools-nuget.

Through registering a Webhook, we can send data (execution context) about any operation performed on Dynamics 365 to the external services or application. The execution context information is passed in JSON format here.

a. Open Plugin Registration tool and Register New WebHook

Passing data from Dynamics 365 to Azure Service Bus Queue using Plugins Workflows

b. Enter details of registration from the “Get function URL” from step 2c

Passing data from Dynamics 365 to Azure Service Bus Queue using Plugins Workflows

5. Register your plugin/workflow assembly.

Working: – In the example, when “Account Number” of an account record is changed, the plugin/workflow is triggered which executes the webhook. The webhook passes the data to azure function and the azure function adds the data to queue. The result can be seen as below in the azure function Monitor section.

Passing data from Dynamics 365 to Azure Service Bus Queue using Plugins Workflows

Passing data from Dynamics 365 to Azure Service Bus Queue using Plugins Workflows

The CRM data is added as a message in the Queue of Azure service Bus as seen in the above screenshot.

Conclusion:

Using the simple steps above user can pass data from Dynamics 365 to Azure Service Bus Queue using Plugins/Workflows.

70% of global 2000 companies apply gamification to improve productivity and returns!

Gamifics365 – Spin the magic of games within Microsoft Dynamics 365 CRM to improve user adoption, enhance productivity, and achieve company goals!

4 thoughts on “Passing data from Dynamics 365 to Azure Service Bus Queue using Plugins/Workflows

  1. George Doubinski

    If the end result is supposed to be a message on a service bus, why not to register a service endpoint in Dynamics and bypass webhook/function structure altogether. Am I missing something?

    George

    1. Inogic

      Yes, you can use Service Endpoint as well to send data from Dynamics 365 to ASB Queue or Topic. But Service Endpoint supports only Asynchronous steps i.e. you cannot register synchronous steps using service endpoint.

      So for Synchronous behavior, you need to use WebHook and azure functions.

      Hope this helps.

      Thanks!

  2. Jørgen Amrud Hagen

    An even easier approach is to register a Service Endpoint through the Plugin registration Tool, and send the Execution COntext directly to the Service Bus Queue, without the need of going through the function app.

  3. Michal

    Hi,
    I agree with Jorgen. You can pass context directly to service bus queue when you register endpoint by Plugin Reg Tool.

Comments are closed.