1

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?

3 Answers 3

5

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.

0

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

0

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

5
  • How does this push subscription notifications to a webhook URL? What URL would you use to setup the webhook/subscription?
    – dvdsmpsn
    Commented Jul 30, 2019 at 0:09
  • 1
    The resource will be: drives/{driveID}/root. The changeType will be: updated. About a subscription url, that's up to you. More information is available in the documentation docs.microsoft.com/en-us/graph/webhooks
    – Tomer
    Commented Jul 30, 2019 at 12:40
  • Thanks. Looking at the page you linked, it says "Using the Microsoft Graph API, an app can subscribe to changes on the following resources" - SharePoint Online drives are not listed. Only OneDrive & OneDrive for Business :( The page is somewhat ambiguous on what is available, so I'll give it another go.
    – dvdsmpsn
    Commented Jul 30, 2019 at 15:12
  • From the link I sent: "Or to the root folder of a SharePoint/OneDrive for Business drive: /drive/root"
    – Tomer
    Commented Jul 30, 2019 at 16:24
  • Hi, I am able to create a subscription on OneDrive, and also I am getting notifications on changes in drive items, but how can I use the notification data to know, which content changed and what are the changes? Please help me, Thanks Commented Mar 17, 2020 at 13:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.