Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 415
TheIronCheek Avatar asked Jun 21 '26 21:06

TheIronCheek


1 Answers

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:

  • utilize search method,for example GET https://graph.microsoft.com/v1.0/drives/{drive-id}/root/search(q='')
  • recursively traverse folder structure via List children of a driveItem endpoint
like image 81
Vadim Gremyachev Avatar answered Jun 24 '26 02:06

Vadim Gremyachev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!