4

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": "me@myorganization.org", "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.

Darrel Miller
  • 139,164
  • 32
  • 194
  • 243
TheIronCheek
  • 1,077
  • 2
  • 20
  • 50

3 Answers3

6

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:

Vadim Gremyachev
  • 57,952
  • 20
  • 129
  • 193
0

Use the "delta" API: https://learn.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

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
Tomer
  • 1,594
  • 14
  • 15
  • That only seems to work for the default drive on the global site, not the sub-site that my document library lives on. See my URLs in the question. – TheIronCheek Sep 16 '19 at 21:09
  • Notice in the URLs in the original question, I'm using /sites/{site-url}:/{subsite-path}:/drives to get my drive list. – TheIronCheek Sep 16 '19 at 21:16
  • Should work fine if you use the drive id (without the site id) + delta. – Tomer Sep 17 '19 at 16:11
0

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>
Yina - MSFT
  • 1,756
  • 1
  • 12
  • 10