Integrating Dynamics 365 with Azure Functions – Part 3

By | June 18, 2018

Integrating Dynamics 365 with Azure Functions Part 3

Introduction:

In our last post about Azure functions, we saw how to register the workflow assembly and execute the workflow using Microsoft Flow in Dynamics 365. Continuing with our series to call webhooks from CRM, let us register the Azure function created as a Webhook. We register this using the Plugin Registration Tool (PRT)

From the PRT choose Register → Register New Webhook

Integrating Dynamics 365 with Azure Functions

In the Endpoint URL provide the function URL.

Choose Authentication method as WebhookKey and provide the key. This key is what you passed as the code querystring earlier.

Note: Webhook is registered as Service End Point in CRM

Integrating Dynamics 365 with Azure Functions

You can now register a step for this webhook just like we do for a plugin assembly.

Integrating Dynamics 365 with Azure Functions

When you create an Account the PluginExecutionContext is received in the message body of the httprequest in your Azure function. You then need to parse that to get context related details.

Once registered as webhook, you can also invoke this through a workflow by executing the endpoint using the following code:

string serviceEndPoint = WebhookURL.Get(executionContext);
                IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>();

                string fetch = @"<fetch>
                  <entity name='serviceendpoint' >
                    <attribute name='serviceendpointid' />
                    <attribute name='name' />
                    <attribute name='authtype' />
                    <attribute name='url' />
                    <filter type='and'>
                      <condition attribute='contract' operator='eq' value='8' />
                      <condition attribute='url' operator='eq' value='" + serviceEndPoint + @"' />
                    </filter>
                  </entity>
                </fetch>";
                EntityCollection coll = crmWorkflowContext.OrganizationService.RetrieveMultiple(new FetchExpression(fetch));

                if (coll != null && coll.Entities.Count > 0)
                {
                    IServiceEndpointNotificationService endpointService =
                             executionContext.GetExtension<IServiceEndpointNotificationService>();

                    crmWorkflowContext.Trace("serviceEndPoint found: " + coll.Entities[0].Id.ToString());
                    endpointService.Execute(coll.Entities[0].ToEntityReference(), context);
                }

Once again you receive the Workflow context in the message body that you can parse to read the information passed.

Conclusion:

There are other ways to invoke Azure functions from D365. Azure functions can be called through Logic Apps and you could register Logic Apps on D365 entities and actions. That’s content for another post. So Keep visiting this space 🙂

Generate Your Own New Leads Now Within Dynamics 365 CRM with Maplytics