In this first post, I would like to show you how we can use Azure Functions as service endpoints for the SharePoint Webhooks subscriptions. I’ll guide you through the process to create a function and I will show you how to form the url that you must set as the endpoint in a webhook subscription.
I’m not going to explain how SP Webhooks work in this post. However, you can do a web search to find out more.
To begin with, you are going to need to create a “Functions App” in Azure. To do this, enter to the Azure Portal and look for “Function App” in the Azure Marketplace:
What you are doing is to create a special kind of App Service, but there is an important thing to notice. If you choose “Consumption Plan” as your Hosting Plan, you will pay only for the time that your functions are being executed. This is an interesting feature of the Azure Functions and it’s worth to use it.
Now if you open the “Function Apps” blade you will find your app in there:
In my case, I’ve created an app with the name “cpichesafunctions”. If you click in the app a “+” button next to the “Functions” will appear. Click on it and choose a template to start. Notice that you can create functions using different programming languages. This time, I’m going to choose C# as the language to use and I want a function of the kind HTTP trigger (there is a “GenericWebHook” template, but it doesn’t work good with SP WebHooks, however, this is just a template and you will write the code that you want). It is important to select “Anonymous” as the authorization level because SharePoint needs it.
Next step is to configure your function (mine is “spwebhookfunction”). Click on “Integrate” and modify “Allowed HTTP methods”, “Mode” and “Selected HTTP methods” like in this image:
Finally, you need the url that you are going to use as the webhook service endpoint. Click on your function to see the “workbench” screen. The code you see in there is the example that the template auto-generates (You will need to modify this to handle SharePoint notifications).
Click on “</> Get function URL” to see the url of your function, copy it in a notepad and add “?code=” at the end:
Now click on the “Manage” button of your function and in the “Function Keys” section click on the “Click to show” link next to the “default” key to see the key:
Add this key at the end of the url that you pasted in your notepad (the key is used to avoid unauthorized usage). What you have is the endpoint url that you must use for your webhook subscription and it should look like this:
"https://cpichesafunctions.azurewebsites.net/api/spwebhookfunction?code=<YOUR KEY>"
In the next post, I will show a functional code example to be used as webhook service endpoint and I will explain what you must do to use environment variables (app settings) and external libraries in your Azure Functions code.