microsoft-graph-apionedrivegraph-explorer

How to search for files in OneDrive using MSGraph query API


Using Graph Explorer I can list files in the "Brendan" directory of my oneDrive. eg The API call below

https://graph.microsoft.com/v1.0/me/drive/root:/Brendan:/children

correctly lists 2 files as "name": "bren1.txt", and "name": "test.pdf",

However if I try to use the query API using this API https://graph.microsoft.com/v1.0/me/drive/root:/Brendan:/children/query with this body

{
    "requests": [
        {
            "entityTypes": [
                "driveItem"
            ],
            "query": {
                "queryString": "bren"
            }
        }
    ]
}

I get

 {
    "error": {
        "code": "itemNotFound",
        "message": "The resource could not be found.",
        "innerError": {
            "date": "2023-04-25T06:03:59",
            "request-id": "e3c688e1-dc08-4302-acdb-1b133a83bae3",
            "client-request-id": "fc6fc88d-38ee-451b-3ada-1914d2f96e91"
        }
    }
}

There are no concrete examples I can find on how to use the MSGraph query API to search for files in directories (driveItems) on OneDrive. Where am I going wrong?

Also ?$filter also seems to work very sporadically, for example:

https://graph.microsoft.com/v1.0/me/drive/root:/Brendan:/children?$filter=startswith(name,'bren')

correctly returns "name": "bren1.txt"

but https://graph.microsoft.com/v1.0/me/drive/root:/Brendan:/children?$filter=endswith(name,'txt') or https://graph.microsoft.com/v1.0/me/drive/root:/Brendan:/children?$filter=contains(name,'bren') both return

{
    "error": {
        "code": "itemNotFound",
        "message": "Item not found",
        "innerError": {
            "date": "2023-04-25T06:13:56",
            "request-id": "28cee436-ec08-4107-8947-380b387ada7d",
            "client-request-id": "6d754b02-aaef-7360-b3ca-c0341a9b6f19"
        }
    }
}

I also tried https://graph.microsoft.com/v1.0/me/drive/root:/Brendan:/children?$search="name:bren1.txt" and https://graph.microsoft.com/v1.0/me/drive/root:/Brendan:/children?$search="bren1.txt"

both of which ignore the search parameter and return everything.

Again Where am I going wrong?


Solution

  • I would prefer using search(q='{search-text}') for searching files.

    GET https://graph.microsoft.com/v1.0/me/drive/root:/Brendan:/search(q='bren1.txt')
    

    The query above will search for items in the "Brendan" directory of your OneDrive. It searches the value across filename, metadata, and file content.

    I think that $filter query is ignored.

    Second option is using /search/query endpoint

    POST https://graph.microsoft.com/v1.0/search/query
    
    {
        "requests": [
            {
                "entityTypes": [
                    "driveItem"
                ],
                "query": {
                    "queryString": "filename:bren1.txt AND path:\"https://tenant-my.sharepoint.com/personal/xxx/Documents/Brendan\""
                }
            }
        ]
    }
    

    You can filter items by file name and scope the query to a particular folder within a OneDrive or site.

    Documentation:

    Search for files

    Filter in search queries