Friday, 5 January 2018

Use Azure Functions as remote event receiver in SharePoint

SharePoint webhooks are a new way of building event receivers for SharePoint. However, webhooks are only enabled for SharePoint list items at the time of writing this.

Recently I was required to write a event receiver for ListAdded event in a web. I decided to try and use Azure functions to achieve this instead of writing the traditional provider hosted app remote event receiver. Came across this great post  from Sergei Sergeev for reference.

Lets create a ListAdded event receiver using Azure Functions.

To Begin with, create a Azure Function project in Visual Studio and created a Function App in Azure through the Azure Portal. Add a function to the function app and select the HTTP trigger type of function since the event receiver needs to send the event payload to a HTTP endpoint. Publish your Azure function to the function app created through Azure Portal. The Azure function is now ready to receive events from SharePoint.

The Azure Function will have an endpoint. This can be fetched by clicking on the function in the Azure portal and clicking on the Get function URL link

The URL will look something like this :


The domain may be different based on your tenant configurations. We need the domain part of this URL in the next steps.

Now we need to register a event receiver on a WEB in SharePoint. We will use a simple console app for this. The console app will use SharePoint App credentials. So, head over to the "/_layouts/AppRegNew.aspx" page in your site and register a new app. Remember to provide the domain of this app as the domain of your event receiver Azure function noted above. We will need this in the future. Provide full control permissions to this APP on the site collection.

Here is the code for installing an event receiver on the web. Replace receiverAzureFuncUrl, webUrl, ClientID and ClientSecret and run the console app. The console app users PnP Sites Core to get app-only client context.

Now the Azure function is ready to receive event of a list being added to the WEB. We need one more thing in our code and that is "TokenHelper". This is used to validate the context token received in the event payload and communicate with SharePoint through from within Azure function (which is our event receiver). I created a class library project in my Azure Function solution in Visual Studio and installed the "AppForSharePointOnlineWebToolKit" nuget package. Add a reference of this class library in your Azure Function APP project

NOTE : I had to add the reference of the DLL of this class library. Adding the project reference made the publish operation to fail.

Since the token helper fetches the ClientID and ClientSecret from webconfig, modify the code to fetch these values from the Azure Function APP settings.

Change the code of your Azure function to handle the incoming request from List Added event. This code reads the event payload and parses the XML to fetch the values that are of our interest. You can log the entire XML and see what are the values received through the Event payload. Once we have the "ContextToken" string, we can Validate the token and also get the client context of the site using "TokenHelper".  The "GetClientContextWithContextToken" method internally validates the token as well.

Hope this helps !

No comments:

Post a Comment