How to search for OneDrive Files within Folders using ParentId via Microsoft Graph Search API?

Clyde Noronha 1 Reputation point
2022-11-08T06:39:19.677+00:00

I'm attempting to search for OneDrive files within particular Folders.

I've managed to accomplish this with the POST /v1.0/search/query API via path: as follows:

{  
    "requests": [  
        {  
            "entityTypes": [  
                "driveItem"  
            ],  
            "query": {  
                "queryString": "test path:(\"https://test-my.sharepoint.com/personal/clyde_noronha/Documents/test/*\" OR \"https://test-my.sharepoint.com/personal/clyde_noronha/Documents/new/*\")"  
            }  
        }  
    ]  
}  

However, I need to do this with the help of only the folder's ID.

According to this page which lists down all Queryable properties for the Graph Search API, 'ParentId' is mentioned.
So I tried using 'parentid:' instead of 'path:' and specified the appropriate folder ID (taken from parentReference.id), however it always returns an empty result. Tried removing the double quotes as well as by capitalizing to 'ParentId', but no luck.

{  
    "requests": [  
        {  
            "entityTypes": [  
                "driveItem"  
            ],  
            "query": {  
                "queryString": "test parentid:\"QRH12ZKP56Y2GOVW7725BZO354PWSJTSQN\""  
            }  
        }  
    ]  
}  

I have tried using the parentReference.driveId as well, but that too returns an empty result.

Am I missing something here? Any help would be greatly appreciated.

Note: I have tried using the GET /v1.0/me/drive/items/QRH12ZKP56Y2GOVW7725BZO354PWSJTSQN/search(q='test') API, however this is not applicable to my use-case as I need to be able to search the same term within multiple folders using a single query.

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
11,052 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Srinivasa Rao Darna 6,701 Reputation points Microsoft Vendor
    2022-11-08T07:38:47.62+00:00

    Hello @Clyde Noronha ,

    Using ParentLink:/FolderName, along with Path should be able to retrieve driveItems from a parent folder within a document library.

    A similar thread graph-api-search-for-driveitems-within-a-drive-ret.html.

    POST https://graph.microsoft.com/v1.0/search/query  
     {  
     "requests": [  
         {  
             "entityTypes": [  
                 "driveItem"  
             ],  
             "query": {  
                 "queryString": "searchstring AND Path:https://contoso.sharepoint.com/sites/siteurl/Shared Documents AND ParentLink:/FolderName"  
             }  
         }  
       ]  
     }  
    

    Hope this helps.
    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    1 person found this answer helpful.