atlas by clearpeople

Azure Functions, SharePoint Webhooks and PnP - Part1

  

I would like to show you how to use Azure Functions along with SharePoint WebHooks. Besides, I will show you how to use environmental variables (app settings) and code from external libraries (like the PnP Core) within the code of your Azure functions.

 

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:

 

Functions App in Azure


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.

 

Functions App in Azure Consumption plan


Now if you open the “Function Apps” blade you will find your app in there:

 

Functions App in Azure Blade


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.

 

Functions App in Azure HTTP Trigger


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:

Functions App in Azure Configure function


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:

 

Functions App in Azure Gen function URL


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:

 

Functions App in Azure Manage


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.

Author bio

Ivan Chesa

Ivan Chesa

I’m part of the SharePoint development team. Besides working on client and internal projects, I enjoy learning more about other technologies like Azure. In my spare time I love playing video games with friends and I also like to watch TV shows.

View all articles by this author View all articles by this author

Get our latest posts in your inbox