How to filter SharePoint document libraries in Microsoft Graph API

SharePoint site's lists

Sometimes, you can have a SharePoint site with multiple lists and you want to retrieve only document libraries.

A list resource has a property called list that contains additional information about the list.

  • contentTypesEnabled: If true, indicates that content types are enabled for this list.
  • hidden: If true, indicates that the list isn't normally visible in the SharePoint user experience.
  • template: Represents the base list template used in creating the list. Value documentLibrary indicates that the list represents document library.

Unfortunately, the template property doesn't support filtering, so you can't filter the document libraries directly like this:

GET https://graph.microsoft.com/v1.0/sites/{site-id}/lists?$filter=list/template eq 'documentLibrary'

Search API

One way to find SharePoint document libraries is to use the search API.

To search for document libraries across all sites where the signed-in user has access, you can use the following query:

POST https://graph.microsoft.com/v1.0/search/query
{
    "requests": [
        {
            "entityTypes": [
                "list"
            ],
            "query": {
                "queryString": "contentclass:STS_List_DocumentLibrary"
            }
        }
    ]
}

If you want to limit the search to a specific site, you can use the path property in the query string.

POST https://graph.microsoft.com/v1.0/search/query
{
    "requests": [
        {
            "entityTypes": [
                "list"
            ],
            "query": {
                "queryString": "contentclass:STS_List_DocumentLibrary AND path:\"https://contoso.sharepoint.com/sites/site-name\""
            }
        }
    ]
}

Drives API

SharePoint document libraries can be accessed as a drive resources. The collection of document libraries (drives) under a SharePoint site can be retrieved

GET https://graph.microsoft.com/v1.0/sites/{site_id}/drives

Each drive resource has a relationship list that contains additional information about the list like it's id.

GET https://graph.microsoft.com/v1.0/sites/{site_id}/drives?$select=name,id&$expand=list($select=id,sharePointIds,list)

The response will contain details about the document libraries in the site.

{
    "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#drives(id,name,list(id,sharepointIds,list))",
    "value": [
        {
            "id": "b!ROvE...HUBHn",
            "name": "Documents",
            "list": {
                "id": "435404ab-5fb8-4232-851e-116316efbb95",
                "list": {
                    "contentTypesEnabled": false,
                    "hidden": false,
                    "template": "documentLibrary"
                },
                "sharepointIds": {
                    "listId": "435404ab-5fb8-4232-851e-116316efbb95",
                    "siteId": "27c4eb44-4a8b-4825-9311-a6837f12aad7",
                    "siteUrl": "https://contoso.sharepoint.com/sites/Management",
                    "tenantId": "abc123ab-c645-d7ab-8499-fb6b763cfdae",
                    "webId": "abc1679b-25d8-1234-87c9-bb0286875011"
                }
            }
        },
        {
            "id": "b!ROvE...qJPR",
            "name": "Reports",
            "list": {
                "id": "78ac44e7-9fd7-465f-9297-3e763da893d1",
                "list": {
                    "contentTypesEnabled": false,
                    "hidden": false,
                    "template": "documentLibrary"
                },
                "sharepointIds": {
                    "listId": "78ac44e7-9fd7-465f-9297-3e763da893d1",
                    "siteId": "27c4eb44-4a8b-4825-9311-a6837f12aad7",
                    "siteUrl": "https://contoso.sharepoint.com/sites/Management",
                    "tenantId": "abc123ab-c645-d7ab-8499-fb6b763cfdae",
                    "webId": "abc1679b-25d8-1234-87c9-bb0286875011"
                }
            }
        }
    ]
}
1
Buy Me a Coffee at ko-fi.com
An error has occurred. This application may no longer respond until reloaded. Reload x