As a SharePoint developer, what you should know about Web-Hooks ?

Sriram Varadarajan
 
Solution Architect
December 2, 2016
 
Rate this article
 
Views
4239

This article on Web hooks for SharePoint developers is a compilation of basic what and why questions related to web hooks.

What is Web Hooks?

In a simple term it’s the way of notification (Push model instead of Pull)

Does Web Hooks belong to MS/SharePoint?

No, Web Hooks is a universal model of notification and MS is just consuming the same.

When to go for Web Hooks?

It’s supports Async event (means ‘ed event) but if you need Synchronous event (‘ing) continue to use remote event receiver.

Are web hooks available in SharePoint online?

Not yet, but it’s available for Outlook and OneDrive

How Web hooks works?

As mentioned earlier it’s a notification service and for us to consume the service we need to do the following:

1. Our application needs to tell SharePoint that you want to have a web hook on particular object (MS à The first version of Web hook will be available for SP List)

2. First step should be Subscribe for a webhook using a REST POST call

        1. [Your application -> SharePoint] POST /_api/web/lists(‘list-id’)/subscriptions
        2. The content type should contain the following:

"resource", (sharepoint URL)

"notificationUrl" and (our Application)

"expirationDateTime"

Example:

 Content-Type: application/json 
 {
     "resource": "https://contoso.sharepoint.com/_api/web/lists({id})",
     "notificationUrl": "https://{your host}/your/webhook/service ",
     "expirationDateTime": "2016-06-27T16:17:57+00:00"
 }

Note : As of now, the expirationPeriod can be of MAX 6 months (Remote Event Receiver doesn’t have this limit)

3. SharePoint Sends a validation token back POST https://{your host}/your/webhook/service?validationToken={randomString}

4. Your web hook notification service end point should Reply 200 OK to Sharepoint along with the randomstring and this should take place with in 5 sec

5. [SharePoint to Your application] Acknowledged by SharePoint

With

1. Response 201 created

2. "id", "expirationDateTime", "notificationUrl" and "resource" sent back as response

3. "id" will be the subscription id

Example:

 Content-Type: application/json
 {
     "id": "a8e6d5e6-9f7f-497a-b97f-8ffe8f559dc7",
     "expirationDateTime": "2016-04-27T16:17:57Z",
     "notificationUrl": " https://{your host}/your/webhook/service ",
     "resource": "{id}"
 }

What happens when a change happens in a SharePoint?

SharePoint Notifies the change to our application like;

POST https://{your host}/your/webhook/service

And our application should return 200 with in 5 seconds

Sample response from SharePoint

 {
     "value": [
         {
             "subscriptionId": "91779246-afe9-4525-b122-6c199ae89211",
             "clientState": "00000000-0000-0000-0000-000000000000",
             "expirationDateTime": "2016-04-30T17:27:00.0000000Z",
             "resource": "b9f6f714-9df8-470b-b22e-653855e1c181", // (LISTID)
             "tenantId": "00000000-0000-0000-0000-000000000000",
             "siteUrl": "/",
             "webId": "dbc5a806-e4d4-46e5-951c-6344d70b62fa"
         }
     ]
 }

Note:

How to consume a WebHooks is beyond the scope of this blog (as it’s very complicated now), we might have to wait for the next release but the core should still remain the same.

Category : Office 365, SharePoint

Author Info

Sriram Varadarajan
 
Solution Architect
 
Rate this article
 
Sriram is a Technology Evangelist with 15+ years experience in Microsoft Technologies. He is an enterprise architect working for large pharmaceutical organization which has presence globally with largest Microsoft implementation ...read more
 

Leave a comment