SharePoint Webhooks were made generally available in January 2017. It provides the mechanism of catching the Asynchronous Events like item added, item deleted etc., that happen in the SharePoint Objects. At present, SharePoint Webhooks cannot be used with Synchronous events like Adding Items, for which we will still have to rely on Remote Event Receiver solution using Provider Hosted Add-in. As of now, SharePoint webhooks are only enabled for SharePoint list items.
Working of SharePoint Webhooks
So as to implement a new SharePoint webhook, we will first have to create a service endpoint using Azure Function and then add it as a subscription to the SharePoint List. While creating the subscription, we will have to specify the below information so that the subscription can be registered with the list.
- Resource - This would be the SharePoint List REST API URL
- Server notification URL – This is the service endpoint URL which we will get after creating the Azure Function App. It is to this URL , SharePoint will send the notification and data payload when ever an event happens in the list.
- Expiration date – It is the expiration date of our subscription which is by default 6 months. It cannot be more than that.
- Client State(Optional) – It is a string passed back to the client on all notifications. We can use this for validating notifications send from SharePoint.
Create Service Endpoint in Azure
We will make use of Azure Functions to create the Service Endpoint that will store the code logic which will be triggered in response to the event notification coming from SharePoint. We will be using the URL received after creating the Azure Function App to add as a new subscription to the List.
To create the Azure Function App, search for ‘Function App’ in the Azure Portal and select it.

Specify the App name and other details required for creating the Azure Function App.

Once created, we can add a HTTP Trigger function as shown below.

We will specify the Trigger Function Name and set the Authorization level to Anonymous.

Upon creation, it will have a default code fragment which we will replace with our code.

We will add the code at a later point. First, we need to make sure that the trigger will respond to some notification it receives. So, let’s go to the "Integrate" section and change the settings as shown below.

In the run.csx file, from the test section add a new query parameter ‘validationtoken’ and set an arbitrary value.

Upon running it if we get the below status in the logs we can make sure that the trigger function is setup for basic use.

It will also give us a 200 OK status in the output window.

After the testing get the Function URL from the run window.

Copy the URL as shown below,

To avoid unauthorized usage of our Azure Function we can specify a secret code which we can get from the Manage section to the end of the SharePoint webhook URL. In our case the SharePoint Webhook URL which we will use to attach to the SharePoint List will look like below:
https://sharepointwebhooks.azurewebsites.net/api/WebhookFunction?code=naSJbEvPNe5mNMVFlL4pehlkAV0HROZqnN6lKIfqrOfNwN

Add SPWebhook Subscription
Once we have got the web hook URL let's register it with the SharePoint List. In this demo we will be registering it with the list named SLA so that whenever a new item is added, we will have to inform the Service User about the new change in the List for him to process the action item before the SLA Date.
So as to add the subscription we will be making use of the below parameters
- Resource - /_api/web/lists(‘SLA List GUID')/subscriptions
- Server notification URL
- Expiration date – "2017-09-19T16:17:57+00:00"
We will be issuing a POST request with the above body. We can either use an Azure AD Application or a SharePoint Hosted Add-in to add the subscription. As per the Microsoft Documentation, ensure that the below permission are assigned so the subscription can be added successfully,
If your application is a Microsoft Azure Active Directory (AD) application
| Application | Permission |
| Office 365 SharePoint Online | Read and write items and lists in all site collections. |
If your application is a SharePoint add-in
You must grant the SharePoint add-in the following permission(s) or higher,
| Scope | Permission Rights |
| List | Manage |
Add SharePoint Webhook Subscription using SPWebhookAdmin
We can either create a new application to register the subscription from scratch as shown in this example. However, for this demo let’s not reinvent the wheel, we will make use of an awesome tool created by Josh that registers the SharePoint WebHook Subscription using SharePoint Framework. He has done an awesome work with SPWebHookAdmin.
You can download the entire source code from GitHub. Lets download it to the desktop and run the SPFx solution to register SharePoint Webhook.

We can download the repository from github

Since this is a SharePoint Framework solution we will need to have the prerequisites required for SPFx setup,which is detailed here. Once the solution is downloaded , open Node JS command prompt and head over to the src folder location of the solution.

Build the code for the first time using the below commands as mentioned in Github.

If we want to view the solution implementation we can use enter Code . to gulp so that the solution opens up in Visual Studio Code.

Lets head over and run gulp serve so that the solution will be added to the local workbench.

In our case we have to add the solution to SharePoint Online. We will do this by appending the below URL to our SharePoint Site Collection URL.
_layouts/15/workbench.aspx

From the + Sign add the Webhook manager to the page.This will list out all the list in the site and the number of subscriptions associated with it.

As we can see SLA list does not have any subscription. Add a new one clicking on the + Sign.

Specify the Notification URL which is our Azure Function URL. Client State is optional as per the documentation, but this solution uses it for additional validations. You can add an arbitrary string here say: ValidationChecker. Set the Expiration date which should not exceed 6 months from the date of creation.

Thus the new subscription has been added and we can see the count updated as below.

On Clicking the list name we can see the newly added subscription.

When the subscription was added to the SharePoint List, in the azure portal the recently created Azure Function was triggered and the we can see the validation token that was received from SharePoint in the Logs section.

Add Client DLLs to Azure Function

















Rohit DevmorePosted Apr 10, 2019, 5:59 AM
Can u please share content of run.csx file
Anjani ChPosted Jul 16, 2018, 10:58 AM
Hi, When I run the SPFx code getting the below error: throw err; ^Error: Cannot find module '@microsoft/sp-build-web' at Function.Module._resolveFilename (module.js:547:15) at Function.Module._load (module.js:474:25) at Module.require (module.js:596:17) at require (internal/module.js:11:18) at Object.<anonymous> (Downloads\spwebhookadmin-master (1)\spwebhookadmin-master\gulpfile.js:4:15) at Module._compile (module.js:652:30) at Object.Module._extensions..js (module.js:663:10) at Module.load (module.js:565:32) at tryModuleLoad (module.js:505:12) at Function.Module._load (module.js:497:3) ........................Kindly suggest