plugin Archives - Microsoft Dynamics 365 Blog http://microsoftdynamics.in/tag/plugin/ Microsoft Dynamics CRM . Microsoft Power Platform Sat, 25 Apr 2020 08:42:04 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.5 https://i0.wp.com/microsoftdynamics.in/wp-content/uploads/2020/04/cropped-Microsoftdynamics365-blogs.png?fit=32%2C32 plugin Archives - Microsoft Dynamics 365 Blog http://microsoftdynamics.in/tag/plugin/ 32 32 176351444 Passing data from Dynamics 365 to Azure Service Bus Queue using Plugins/Workflows http://microsoftdynamics.in/2018/08/16/passing-data-from-dynamics-365-to-azure-service-bus-queue-using-plugins-workflows/ Thu, 16 Aug 2018 12:18:01 +0000 https://www.inogic.com/blog/?p=12658 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...

The post Passing data from Dynamics 365 to Azure Service Bus Queue using Plugins/Workflows appeared first on Microsoft Dynamics 365 Blog.

]]>
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.

Integrate Dynamics CRM Online QuickBooks

Source

The post Passing data from Dynamics 365 to Azure Service Bus Queue using Plugins/Workflows appeared first on Microsoft Dynamics 365 Blog.

]]>
4360
Create Your First Plugin Dynamics Microsoft Dynamics CRM – Part 1 http://microsoftdynamics.in/2017/12/20/create-your-first-plugin-dynamics-ms-crm/ Wed, 20 Dec 2017 09:39:00 +0000 http://microsoftdynamics.in/2017/12/20/create-your-first-plugin-dynamics-ms-crm/ For your first Plugin we will take a test scenario – on create of contact record our plugin will forcefully update its email address to “testemail@gmail.com” and to achieve this you just need to follow the below steps. There are two parts to successfully run a plugin in MSCRM 1) Create Your Plugin 2) Resister...

The post Create Your First Plugin Dynamics Microsoft Dynamics CRM – Part 1 appeared first on Microsoft Dynamics 365 Blog.

]]>
For your first Plugin we will take a test scenario – on create of contact record our plugin will forcefully update its email address to “testemail@gmail.com” and to achieve this you just need to follow the below steps.
There are two parts to successfully run a plugin in MSCRM
1) Create Your Plugin
2) Resister your plugin
Plugin Creation
 
Step 1 – Open Visual studio
Step 2 – Go to File – New – and Click on Project

 

Step 3 – A dialogue will appear, from left navigation panel select Class Library template in Visual C#  and Enter Project and Solution name and hit on OK Button

Step 4 – Visual studio will create and open a blank class library template.Right Click on Project and click on Add References option

Step 5 – Reference Manager dialogue will open.From left navigation panel go to Assemblies – Framework then select two references and click on OK button
1)System.Runtime.Serialization
2) System.ServiceModel

Step 6 –  Right Click on Project and again click on Add References option, then in Reference Manager dialogue from left navigation panel go to Browse – Click on Browse button then select two references
1)System.Runtime.Serialization
2) System.ServiceModel

Step 7 –  After Click on Browse button go to your sdk (downloaded from prerequisites) – bin, then select the following dlls files and click on add and then OK button
1) miscrosoft.xrm.client.dll
2) Microsoft.Xrm.Sdk.dll

Step 8 –  Open the class named Class.cs from solution explorer and modify the below code in it.
If your namespace name and class name is different then remember to verify and update it.

Code Snippet : 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xrm.Sdk;
namespace My_First_Plugin
{
    public class Class1 : IPlugin     //{inherit the class Iplugin in your class}
    {
        public void Execute(IServiceProvider serviceProvider)        //{add the execute method}
        {
            // create context , service factory and service objects
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(Microsoft.Xrm.Sdk.IPluginExecutionContext));
            IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
            //to enter  value in Email field in contact entity
            //create an object of predefined class Entity and pass entity name as its parameter
            Entity Object = new Entity(“contact”);   // in place of contact you can write schema name of entity on which you want to preform operations
            Object.Attributes.Add(“emailaddress1”, “testemail@gmail.com”);  //emailaddress1 is the schema name of the Email field in contact entity
             //testemail@gmail.com is the value
            //to update the record service.Update(object of the entity)
            service.Update(Object);
        }
    }
}
After updating your code your code will look like this

Step 9 –  Right click on Project and click on Properties

Step 10 –  Now, we have to create a key. Go to Signing – select the checkbox “Sign the assembly” and click on <New..> option
Step 11 –  Enter any name you want for your Key and click on OK button

Step 12 –  Click on Save Button

Step 13 – Go to BUILD from ribbon menu and select Build Solution option

 

The post Create Your First Plugin Dynamics Microsoft Dynamics CRM – Part 1 appeared first on Microsoft Dynamics 365 Blog.

]]>
2764