Call Azure Function from Dynamics 365 CRM using Webhooks

This is a vast topic to cover in a blog. But I wanted to write from a bird-eye’s view of how this will pan out in an implementation where you perform a certain operation in Dynamics 365 CRM and an Azure Function is called to perform further operations.

This post is written keeping in mind fair knowledge of Azure Functions, Storage accounts and subscriptions in mind.

I’ll try to keep the article short, so stay with me! 🙂

Create a Function App in Azure

  1. Let’s say you have created a Function App in Azure already and want to connect to Dynamics 365 CRM. Click on the big + New Function button in the screenshot below
    resourceOverview_LI
  2. Now, since I want to keep Visual Studio as my driver for coding and deployment, I’ll create a new Project in Visual Studio of type Azure Functions and click Next
    newProj
  3. On the next page, I’ll give a relevant name and hit Create.
    createProjectButton
  4. Since we will be using Webhooks to connect to the Azure Function, the trigger chosen here is Http Trigger.Make sure you select Framework because Microsoft.Xrm.Sdk assemblies don’t work on .NET Code, but on .NETFramework only.And then you should take care of what your Storage Account and Authorization should be – Finally click Create once done.
    frameWorkSelected
  5. The Project will open with 1 .cs file, so make sure you name your plugin initially –
    accountPluginGetsInApp
  6. It’ll take a while to create the Project. Once created, go to the Portal on Azure and click on the Get Publish Profile
    getPublishProf_LI
    It will be downloaded on the computer. Keep it so that you can import it on the Project in Visual Studio to use for Direct Publish.
    downloadedProfile
  7. Now, right click and chose Publish to make your first push as is so that the Account Function gets pushed to Azure App.
    firstPublish
  8. Then, click on Import and import the Publish Profile settings downloaded in step #5 above –
    selectImport
  9. Once imported, you’ll be taken here – simply Publish once.
    quickPublish
  10. Once Publish is successful, check in the Azure App in Portal, the Function should appear.
    accountPluginGetsInApp

 

Modifying code to read Webhook Call from Dynamics 365

  1. To keep it simple, I’m simply reading the context and then, you can flourish your App further to make it work as required.
    captureContextSo, I’m only reading the request into a String and logging it so that we can see it in the logs in the Azure Function app.
  2. You can use RemoteExecutionContext class to actually get all the contextual information into the Function app and then use it further. See below –
    remotecontext
  3. Once ready with your code, Publish it.

Now, let’s Register the Webhook and call the app.

If you’re also looking for remote debugging, this is a great article-Remote Debugging Azure Functions V2 “The breakpoint will not currently be hit. No symbols have been loaded for this document”

Register a Webhook in Plugin Registration Tool

Coming to Dynamics 365 CRM side of things, you can register a Webhook that will trigger on Account Name update to fire off the AccountPlugin Function App created above –

  1. In Plugin Registration Tool, register a new Webhook
    registerWebHook
  2. Enter Webhook Details. Select Authentication type as WebhookKey
    enterWebhookDetails
  3. Now, to get the key, go to the Function App in portal, and look </> Get function URL link.
    getKeyandURL
  4. Copy the same and paste in Notepad, separate the code part from the main URL
    selectCopy
    separateCodePart
  5. Paste the URL part in Endpoint URL and key in the Value field. Click Save.
    registerWebHookWIthDetails
  6. Now, add a Step to the Webhook. For this example, I’ve chosen update of Account‘s Account Name field
    addStepregisterStepInWebhook
    And Register it.

Execution

  1. The purpose was to simply ready Dynamics 365 Account record upon modification of the Account Name
    recordChange
    And save the record.
  2. In a minute or so, the Log will be generated (only the logs take a little longer to generate)
    triggered
    And thus, we are able to send data / or rather, call Azure Function and process Dynamics 365 CRM data using Webhooks.

Some other Azure related post you might like to look at – Use Azure App Passwords for MFA enabled D365 authentication from Console App

Hope this was helpful! Tried my best to keep it basic and short as possible. I’m sure you all will explore way beyond and develop awesome implementations!

10 thoughts on “Call Azure Function from Dynamics 365 CRM using Webhooks

      • Yes, its ribbon button. Though we are able to trigger the function using function url from Postman or from browser or Plugin, but when we are calling it from CRM JS (code attached), it’s giving “BAD Request / Called Failed” error. Do you know if it’s supported to call from JS?

        Like

  1. Hi,
    Can we implement synchronous pattern using this approach? Or the call to Azure Function from a plugin using Webhooks is necessarily a Async pattern always?

    Like

    • So I made a Sync call. you can even have it Async. I’ve seen someone create a plugin and call it async/sync. But I can’t be sure since I haven’t tried personally. But it looks very much possible.

      Like

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.