{"id":12658,"date":"2018-08-16T17:48:01","date_gmt":"2018-08-16T12:18:01","guid":{"rendered":"https:\/\/www.inogic.com\/blog\/?p=12658"},"modified":"2022-07-21T12:06:25","modified_gmt":"2022-07-21T06:36:25","slug":"passing-data-from-dynamics-365-to-azure-service-bus-queue-using-pluginsworkflows","status":"publish","type":"post","link":"https:\/\/www.inogic.com\/blog\/2018\/08\/passing-data-from-dynamics-365-to-azure-service-bus-queue-using-pluginsworkflows\/","title":{"rendered":"Passing data from Dynamics 365 to Azure Service Bus Queue using Plugins\/Workflows"},"content":{"rendered":"<h2><strong>Introduction:<\/strong><\/h2>\n<p style=\"text-align: justify;\">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.<\/p>\n<p style=\"text-align: justify;\">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).<\/p>\n<p style=\"text-align: justify;\"><img decoding=\"async\" class=\"aligncenter wp-image-12659\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2018\/08\/1Passing-data-from-Dynamics-365-to-Azure-Service-Bus-Queue-using-Plugins-Workflows.png\" alt=\"Passing data from Dynamics 365 to Azure Service Bus Queue using Plugins Workflows\" width=\"969\" height=\"563\" \/><\/p>\n<h2>The detailed steps are as follows:-<\/h2>\n<p><strong>1<\/strong>. Create an Azure Service Bus:-<\/p>\n<p>Open Azure portal in your CRM organization and Create Service Bus Namespace by navigating to + Create a Resource &gt;&gt; Integration &gt;&gt; Service Bus<\/p>\n<p><strong>2<\/strong>. Create an Azure function which will add the CRM data to Azure Service Bus Queue:-<\/p>\n<p style=\"padding-left: 30px;\"><strong>a<\/strong>. Navigate to + Create a Resource &gt;&gt; Compute &gt;&gt; Function App<\/p>\n<p style=\"padding-left: 30px;\"><strong>b<\/strong>. Create a C# HttpTrigger Function in it<\/p>\n<p style=\"padding-left: 30px;\"><strong>c<\/strong>. Click on \u201cGet function URL\u201d link<\/p>\n<p><img decoding=\"async\" class=\"aligncenter wp-image-12660\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2018\/08\/2Passing-data-from-Dynamics-365-to-Azure-Service-Bus-Queue-using-Plugins-Workflows.png\" alt=\"Passing data from Dynamics 365 to Azure Service Bus Queue using Plugins Workflows\" width=\"969\" height=\"453\" \/><\/p>\n<p><img decoding=\"async\" class=\"aligncenter wp-image-12661\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2018\/08\/3Passing-data-from-Dynamics-365-to-Azure-Service-Bus-Queue-using-Plugins-Workflows.png\" alt=\"Passing data from Dynamics 365 to Azure Service Bus Queue using Plugins Workflows\" width=\"971\" height=\"279\" \/><\/p>\n<p>This URL link is important and will be used in CRM Plugin\/Workflow code and webhook registration.<\/p>\n<p style=\"padding-left: 30px;\"><strong>d<\/strong>. Click on Integrate and + New Output to create a queue in ASB and provide it to Azure function<\/p>\n<p><img decoding=\"async\" class=\"aligncenter wp-image-12662\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2018\/08\/4Passing-data-from-Dynamics-365-to-Azure-Service-Bus-Queue-using-Plugins-Workflows.png\" alt=\"assing data from Dynamics 365 to Azure Service Bus Queue using Plugins Workflows\" width=\"972\" height=\"451\" \/><\/p>\n<p style=\"padding-left: 30px;\"><strong>e<\/strong>. Select Azure Service Bus<\/p>\n<p><img decoding=\"async\" class=\"aligncenter wp-image-12663\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2018\/08\/5Passing-data-from-Dynamics-365-to-Azure-Service-Bus-Queue-using-Plugins-Workflows.png\" alt=\"Passing data from Dynamics 365 to Azure Service Bus Queue using Plugins Workflows\" width=\"972\" height=\"455\" \/><\/p>\n<p style=\"padding-left: 30px;\"><strong>f<\/strong>. Select \u201cService Bus Queue\u201d in Message Type. You can select the \u201cService Bus Connection\u201d by clicking on new and selecting the desired Service Bus.<\/p>\n<p><img decoding=\"async\" class=\"aligncenter wp-image-12664\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2018\/08\/6Passing-data-from-Dynamics-365-to-Azure-Service-Bus-Queue-using-Plugins-Workflows.png\" alt=\"Passing data from Dynamics 365 to Azure Service Bus Queue using Plugins Workflows\" width=\"975\" height=\"456\" \/><\/p>\n<p style=\"padding-left: 30px;\"><strong>g<\/strong>. Click on the function \u201c<em>f<\/em> functionname\u201d and paste the following code to add the JSON data from CRM to the ASB Queue in the azure function. Save the code.<\/p>\n<pre class=\"lang:default decode:true\">using System;\r\nusing System.Net;\r\npublic static async Task&lt;object&gt; Run(HttpRequestMessage req,IAsyncCollector&lt;Object&gt; outputSbMsg, TraceWriter log)\r\n{\r\n    log.Info($\"Webhook was triggered!\");\r\n    string jsonContent = await req.Content.ReadAsStringAsync();\r\n    log.Info(\"jsonContent \" + jsonContent);\r\n    await outputSbMsg.AddAsync(jsonContent);\r\n    log.Info(\"added to queue\");    \r\n    return req.CreateResponse(HttpStatusCode.OK);\r\n}<\/pre>\n<p>Here,\u00a0\u00a0 \u00a0\u00a0HttpRequestMessage req is \u00a0JSON data from CRM<\/p>\n<p>IAsyncCollector&lt;Object&gt; outputSbMsg \u00a0is \u00a0ASB Queue name<\/p>\n<p><img decoding=\"async\" class=\"aligncenter wp-image-12665\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2018\/08\/7Passing-data-from-Dynamics-365-to-Azure-Service-Bus-Queue-using-Plugins-Workflows.png\" alt=\"Passing data from Dynamics 365 to Azure Service Bus Queue using Plugins Workflows\" width=\"971\" height=\"484\" \/><\/p>\n<p><strong>3<\/strong>. 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:-<\/p>\n<pre class=\"lang:default decode:true \">\/\/ Create a Class having members as data attributes to be passed to Azure\r\n[DataContract]\r\npublic class AccountObj\r\n{\r\n[DataMember]\r\npublic string AccountID { get; set; }\r\n[DataMember]\r\npublic string Name { get; set; }\r\n[DataMember]\r\npublic string Telephone { get; set; }\r\n}\r\n\/\/ Insert the following code after getting the primary entity in Plugin\/workflow\r\nusing System.IO;\r\nusing System.Net;\r\nusing System.Runtime.Serialization.Json;\/\/Install from NuGet Package Manager\r\n\r\nusing (WebClient client = new WebClient())\r\n{\r\n\/\/ Prepare Json data                       \t\r\nDataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(AccountObj));\r\nMemoryStream memoryStream = new MemoryStream();\r\nserializer.WriteObject(memoryStream, accObj);\r\nvar jsonObject = Encoding.Default.GetString(memoryStream.ToArray());\r\n\r\n\/\/ Prepare WebHook\r\nvar webClient = new WebClient();\r\nwebClient.Headers[HttpRequestHeader.ContentType] = \"application\/json\";\r\n\r\n\/\/ Azure Function key\r\nvar code = \"xPhPPB5tGCq86NRbe7wzgJika3bv4ahP9kw7xe5Asoja2vEk4fPqVw==&amp;clientId=default\";\r\n\/\/ Azure Function Url\r\nvar serviceUrl = \"https:\/\/callwebhookfromcrmassebly.azurewebsites.net\/api\/GenericWebhookCSharp1?code=\" + code;\r\n\r\n\/\/ upload the json data to the serviceurl \r\nstring response = webClient.UploadString(serviceUrl, jsonObject);\r\n  }\r\n<\/pre>\n<p><strong>4<\/strong>. Webhook Registration:-<\/p>\n<p>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\u00a0\uf0e0<\/p>\n<p><a href=\"https:\/\/docs.microsoft.com\/en-us\/dynamics365\/customer-engagement\/developer\/download-tools-nuget\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/docs.microsoft.com\/en-us\/dynamics365\/customer-engagement\/developer\/download-tools-nuget<\/a>.<\/p>\n<p>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.<\/p>\n<p style=\"padding-left: 30px;\"><strong>a<\/strong>. Open Plugin Registration tool and Register New WebHook<\/p>\n<p><img decoding=\"async\" class=\"aligncenter wp-image-12666\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2018\/08\/8Passing-data-from-Dynamics-365-to-Azure-Service-Bus-Queue-using-Plugins-Workflows.png\" alt=\"Passing data from Dynamics 365 to Azure Service Bus Queue using Plugins Workflows\" width=\"968\" height=\"501\" \/><\/p>\n<p style=\"padding-left: 30px;\"><strong>b<\/strong>. Enter details of registration from the \u201cGet function URL\u201d from step 2c<\/p>\n<p><img decoding=\"async\" class=\"aligncenter wp-image-12667\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2018\/08\/9Passing-data-from-Dynamics-365-to-Azure-Service-Bus-Queue-using-Plugins-Workflows.png\" alt=\"Passing data from Dynamics 365 to Azure Service Bus Queue using Plugins Workflows\" width=\"968\" height=\"666\" \/><\/p>\n<p><strong>5<\/strong>. Register your plugin\/workflow assembly.<\/p>\n<p>Working: &#8211; In the example, when \u201cAccount Number\u201d 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.<\/p>\n<p><img decoding=\"async\" class=\"aligncenter wp-image-12668\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2018\/08\/10Passing-data-from-Dynamics-365-to-Azure-Service-Bus-Queue-using-Plugins-Workflows.png\" alt=\"Passing data from Dynamics 365 to Azure Service Bus Queue using Plugins Workflows\" width=\"969\" height=\"420\" \/><\/p>\n<p><img decoding=\"async\" class=\"aligncenter wp-image-12669\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2018\/08\/11Passing-data-from-Dynamics-365-to-Azure-Service-Bus-Queue-using-Plugins-Workflows.png\" alt=\"Passing data from Dynamics 365 to Azure Service Bus Queue using Plugins Workflows\" width=\"976\" height=\"464\" \/><\/p>\n<p>The CRM data is added as a message in the Queue of Azure service Bus as seen in the above screenshot.<\/p>\n<h2><strong>Conclusion:<\/strong><\/h2>\n<p>Using the simple steps above user can pass data from Dynamics 365 to Azure Service Bus Queue using Plugins\/Workflows.<\/p>\n<h2 style=\"text-align: left;\"><div class=\"su-heading su-heading-style-default su-heading-align-center\" id=\"\" style=\"font-size:15px;margin-bottom:5px\"><div class=\"su-heading-inner\">70% of global 2000 companies apply gamification to improve productivity and returns!<\/div><\/div><\/h2>\n<p><em><strong><a href=\"https:\/\/bit.ly\/3RD4lYW\" target=\"_blank\" rel=\"noopener noreferrer\">Gamifics365<\/a> <\/strong>\u2013 Spin the magic of games within Microsoft Dynamics 365 CRM to improve user adoption, enhance productivity, and achieve company goals!<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.inogic.com\/blog\/2018\/08\/passing-data-from-dynamics-365-to-azure-service-bus-queue-using-pluginsworkflows\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":13,"featured_media":12670,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[5,42,67],"tags":[190,1847],"class_list":["post-12658","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-azure-functions","category-plugin","category-workflows","tag-azure-service-bus-queue","tag-workflows-2"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/12658","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/comments?post=12658"}],"version-history":[{"count":0,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/12658\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/media\/12670"}],"wp:attachment":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/media?parent=12658"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/categories?post=12658"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/tags?post=12658"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}