Search code examples
microsoft-graph-apiodata

MS Graph /drives/{drive-id}/items/{item-id}/children filter not folders


I need to get all files (filter out folders) from /drives/{drive-id}/items/{item-id}/children request

I can easily get only folders with /drives/{drive-id}/items/{item-id}/children?$filter=folder ne null but when I want to reverse the condition then error is thrown:

The request is not supported by the system.

I tried:

/drives/{drive-id}/items/{item-id}/children?$filter=folder eq null

/drives/{drive-id}/items/{item-id}/children?$filter=not(folder ne null)

/drives/{drive-id}/items/{item-id}/children?$filter=file ne null - for some reason file property cannot be used as opposed to folder

simplified File object:

            "createdDateTime": "2023-03-06T14:31:51Z",
            "eTag": "\"{D1EAF39B-B391-4BFD-926D-A4FE986AEE6E},2\"",
            "id": "02DDZDZCM36PVNDEND7VFZE3NE92MGVPTO",
            "lastModifiedDateTime": "2023-02-20T11:03:34Z",
            "name": "someworkbook.xlsx",
            "file": {
                "mimeType": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
                "hashes": {
                    "quickXorHash": "zjhLucU2pN7vTR8p39LRh7W6C0o="
                }
            },
            "shared": {
                "scope": "users"
            }

simplified Folder object:

            "createdDateTime": "2023-03-06T14:31:18Z",
            "id": "01ADZDZCKYWXT5BK6AQFHIVOACUHRM4BFE",
            "lastModifiedDateTime": "2023-03-06T14:31:17Z",
            "name": "Folder A",
            "folder": {
                "childCount": 0
            },
            "searchResult": {}

Solution

  • I'm afraid you have to use another Graph API endpoint

    POST https://graph.microsoft.com/v1.0/search/query
    {
        "requests": [
            {
                "entityTypes": [
                    "driveItem"
                ],
                "query": {
                    "queryString": "path:\"https://<tenant>.sharepoint.com/sites/<site_name>/path/to/folder\" AND isDocument=true"
                }
            }
        ]
    }
    

    in path you need to specify server path to the destination folder. isDocument=true will ensure that only files are returned in the response.