Question

In the documentation Use the Microsoft Graph API to get change notifications, it says:

Using the Microsoft Graph API, an app can subscribe to changes on the following resources:

  • ...
  • Content within the hierarchy of the root folder driveItem on OneDrive for Business
  • ...

There is no explicit mention of SharePoint Online support for file notifications in the documentation, but because the OneDrive for Business and SharePoint Online APIs within the Microsoft Graph are essentially the same, should this infer that the same functionality is supported for SharePoint Online too?

Was it helpful?

Solution 2

As of June 2019, the answer is no.

There is a single endpoint in the Microsoft Graph with which you can create webhooks (subscriptions): https://graph.microsoft.com/v1.0/subscriptions

This is not specific to OneDrive for Business, but common to all applications in the Microsoft Graph (well, those that support webhooks, that is).

The Microsoft Graph Explorer is a great help to test things without writing too much code.

A successful POST to create a webhook looks something like this:

Create webhook on the Microsoft Graph

Note that the resource is me/drive/root for OneDrive for Business. There are no supported resource values for SharePoint Online at this point.


Aside

  1. OneDrive for Business only supports a changeType of updated even though created and deleted are listed as supported values in the documentation.
  2. You can GET the same URL to list current webhooks/subscriptions:

View subscriptions

OTHER TIPS

I just implemented such a feature for our webapp accessing a customer's SharePoint site. So to make the process for anyone searching for this a bit less painful than it was for me, here's a quick rundown:

Get Notified of changes

You subscribe to the SharePoint drive root folder just like a OneDrive root folder, i.e. at /drives/{driveid}/root with a POST request like so:

POST https://graph.microsoft.com/v1.0/subscriptions
Content-Type: application/json
{
  "changeType": "updated",
  "notificationUrl": "{notificationUrl}",
  "resource": "/drives/{driveId}/root",
  "expirationDateTime": "{expirationDateTime}",
  "clientState": "{secretClientState}"
}

The only changeType supported for a driveItem is "updated" ( https://docs.microsoft.com/en-us/graph/api/resources/subscription?view=graph-rest-1.0#properties ) and on SharePoint / OneDrive for Business you can only subscribe to the root folder ( https://docs.microsoft.com/en-us/graph/api/subscription-get?view=graph-rest-1.0&tabs=http#driveitem ).

The expirationDateTime is only valid if set at maximum 4230 minutes in the future! So you'd have to renew the subscription almost daily to keep it alive. ( https://docs.microsoft.com/en-us/graph/api/resources/subscription?view=graph-rest-1.0#maximum-length-of-subscription-per-resource-type )

Find out what has changed

Microsoft does not deliver resource metadata with the update notification, so for that you'd have to leverage the delta API. (https://docs.microsoft.com/en-us/graph/api/driveitem-delta?view=graph-rest-1.0&tabs=http)

Essentially, you get a token representing a baseline state and when you query the API again with this token, it returns the resources that have changed since then.

So you get a token at a given time

GET https://graph.microsoft.com/v1.0/drives/{driveId}/root/delta?token=latest

and then, when you get a change notification after that, you query the API with that token to find out what has happened.

After this, you get another delta token so you're up to date. Repeat. (This is simplified a bit, you can also get a full list of items as a baseline, but we only care about new items). https://docs.microsoft.com/en-us/graph/api/driveitem-delta?view=graph-rest-1.0&tabs=http#retrieving-the-current-deltalink


See https://docs.microsoft.com/en-us/graph/webhooks and https://docs.microsoft.com/en-us/graph/delta-query-overview for an overview.

The answer is "yes".

The drives associated with (Sharepoint) sites work the same way as OneDrive for business drive (associated with users).

I can confirm (after testing) that it works in the same manner and that notifications work well. (At least the same as drives associated with users).

To get the drives associated with a site use https://docs.microsoft.com/en-us/graph/api/drive-list?view=graph-rest-1.0&tabs=http

E.g.: GET /sites/{siteId}/drives

This will return the drives IDs associated with the site (there may be multiple, in contrast to OneDrive that has 0 or 1).

You may then apply the API to them as you would any other drive. E.g.: GET /drives/{drive-id}/root/delta

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top