'Get SharePoint folders and docs using Microsoft Graph API

I'm trying to get the folders and documents from a SharePoint document library using Microsoft Graph API.

If I do a GET request for https://graph.microsoft.com/v1.0/sites/mysite.sharepoint.com:/sites/MyDocumentSite:/drives/, I get this back:

{
    "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#drives",
    "value":[
        {
            "createdDateTime": "2019-09-05T07:09:49Z",
            "description": "",
            "id": "b!wxh2ZWwoT0KKTdLRYjD5jvzjo8jkat5LgY3VyfgEqkv3YVg_XXXXXXXXXXXXXXXX",
            "lastModifiedDateTime": "2019-09-05T07:09:49Z",
            "name": "Documents",
            "webUrl": "https://mysite.sharepoint.com/sites/MyDocumentSite/Shared%20Documents",
            "driveType": "documentLibrary",
            "createdBy":{"user":{"displayName": "System Account" }},
            "lastModifiedBy":{"user":{"email": "[email protected]", "id": "73f9990c-5c92-4839-8b13-XXXXXXXXXXXX", "displayName": "John Smith"…},
            "quota":{"deleted": 0, "remaining": 0, "total": 0, "used": 0}
        }
    ]
}

But if I try to access that drive by doing a GET request on that id: https://graph.microsoft.com/v1.0/sites/mysite.sharepoint.com:/sites/MyDocumentSite:/drives/b!wxh2ZWwoT0KKTdLRYjD5jvzjo8jkat5LgY3VyfgEqkv3YVg_XXXXXXXXXXXXXXXX, I get a BadRequest error:

{
    "error":{
        "code": "BadRequest",
        "message": "Url specified is invalid.",
        "innerError":{
            "request-id": "7c9eaf61-764f-4d72-abdb-ffa2fe868e90",
            "date": "2019-09-16T19:09:41"
        }
    }
}

Ultimately, I want a way to display all folders and documents from the document library but I can't seem to get past this initial step.



Solution 1:[1]

Indeed, when site is addressed by site path, the following query fails with the exception:

GET `https://graph.microsoft.com/v1.0/sites/{hostname}:/{server-relative-path}/drive/root/children` 

It appears to be a bug since the similar query but when site is addressed by id, is working as expected:

GET https://graph.microsoft.com/v1.0/sites/{site-id}/drive/root/children

Another option to consider, to get Drive resource by its identifier (without specifying site path or identifier), for example:

GET https://graph.microsoft.com/v1.0/drives/{drive-id}/root/children

When it comes the turn of getting all the documents and folder within a library, List children of a driveItem endpoint returns only 1 level beneath the current folder.

To return all the drive items, you could at least consider to:

Solution 2:[2]

Once you get the drive id you want to query, use /drives at the root to make your request:

https://graph.microsoft.com/v1.0/drives/<driveId>

Solution 3:[3]

Use the "delta" API: https://docs.microsoft.com/en-us/graph/api/driveitem-delta?view=graph-rest-1.0&tabs=http

The first call will return all the items. (If there is pagination you'll need to call nextLink, until you receive a deltaLink).

You will probably want to use one either GET /sites/{siteId}/drive/root/delta or GET /drives/{drive-id}/root/delta

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Vadim Gremyachev
Solution 2 Yina - MSFT
Solution 3 ΩmegaMan