Search code examples
microsoft-graph-apimicrosoft-graph-files

Enumerate DriveItem resources of a specific Drive given SharePoint URL and using Graph API


I have a SharePoint URL of the form https://organizationname.sharepoint.com/sites/....

I want to use the Graph API to get a list of all resources in this drive. Reading the API documentation it appears that I require the drive-id of this drive in order to perform this request.

/drives/{drive-id}/root/children

Also, according to the answer to a similar stackoverflow question it appears there are no APIs to convert SharePoint URL to OneDrive driveId. Is there a possible workaround? Is there any way to programmatically get a list of resources from a SharePoint URL?


Solution

  • If your SharePoint URL is https://organizationname.sharepoint.com/sites/yourSiteName, then you can issue a request like this via the Graph API (scope Sites.Read.All might be required):

    client.api("/sites/organizationname.sharepoint.com:/sites/yourSiteName:/drives").get();
    

    That request will return something like this:

    {
        "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#drives",
        "value": [
            {
                "createdDateTime": "2021-07-24T23:35:00Z",
                "description": "",
                "id": "b!A1234567-ZYXWVUTSRQPONMLKJIHGFEDCBA9876543210",
                "lastModifiedDateTime": "2021-08-12T16:39:23Z",
                "name": "Dokumente",
                "webUrl": "https://organizationname.sharepoint.com/sites/yourSIteName/folderName",
                "driveType": "documentLibrary",
                "createdBy": {
                    "user": {
                        "displayName": "abc"
                    }
                },
                "lastModifiedBy": {
                    "user": {
                        "email": "[email protected]",
                        "id": "12345678-4321-4321-4321-012345678901",
                        "displayName": "zz"
                    }
                },
                "owner": {
                    "group": {
                        "email": "[email protected]",
                        "id": "09876543-1234-1234-1234-012345678901",
                        "displayName": "Owner of something"
                    }
                },
                "quota": {
                    "deleted": 345678,
                    "remaining": 27487788453406,
                    "state": "normal",
                    "total": 27487790694400,
                    "used": 96120
                }
            }
        ]
    }
    

    The id under description is the drive-id. With that you can get /root/children like so:

    client.api("/sites/yourorganizationname.sharepoint.com/drives/b!A1234567-ZYXWVUTSRQPONMLKJIHGFEDCBA9876543210/root/children").get();
    

    While there is no single API or algorithm that allows you to programmatically get a list of resources from a SharePoint URL, you can achieve the same with two Graph API requests.