user1452215
user1452215

Reputation: 640

Microsoft graph search within drive or folder hierarchy returns nothing

I'm doing search in drive and folder hierarchy in the graph explorer logged in as myself and having granted consent and nothing is being returned.

On this page: https://learn.microsoft.com/en-us/graph/api/driveitem-search?view=graph-rest-1.0

It says: "Search the hierarchy of items for items matching a query. You can search within a folder hierarchy, a whole drive, or files shared with the current user."

When I do search based on what the user can access documented in this section "Searching for items a user can access" then I get results. But if I specify the root or some other folder then the search returns nothing, i.e. this works:

/me/drive/search(q='{search-query}')

but these return nothing even though I'm searching within my drive and have items in my drive owned by me that meet the search criteria:

/me/drive/root/search(q='{search-text}')

/drives/{drive-id}/root/search(q='{search-text}')

/drives/{drive-id}/items/{folder-id}/search(q='{search-text}')

Does anyone have any insight into this problem? When I get the results from the search on what I can see, the results clearly show that the results are from my drive. And as I mentioned those items are owned by me.

Update: strange but testing again today, the root and folder search seem to be working now. We're going t continue to monitor the results.

Regards, LT

Upvotes: 4

Views: 2029

Answers (3)

w. Patrick Gale
w. Patrick Gale

Reputation: 2307

Before you start integrating your API call into you code, save yourself the headache and make sure the API call works using the Graph Explorer at https://developer.microsoft.com/en-us/graph/graph-explorer. This will ensure you know the permissions that are needed to make the API call and ensure the API call works as expected.

With regards to searching folder hierarchy, one of the first problems you need to solve is what is the root folder named within your drive. I start by calling the following endpoint using Graph Explorer and visiting the webUrl from the response.

https://graph.microsoft.com/v1.0/drives/{drive ID}/root

In my case I have a Data legacy directory in the root folder, so I can modify my API request to use the following to narrow down my results to only those under the Data legacy directory:

https://graph.microsoft.com/v1.0/drives/{drive ID}/root:/Data legacy 

The first :/ string after root simply tells the Graph API that we are beginning to specify a path under the root to return results. In the case above it will simply return a description of that directory (not the contents). If we want to see the children under that directory we would modify the endpoint to use:

https://graph.microsoft.com/v1.0/drives/{drive ID}/root:/Data legacy:/children

The second :/ tells the API that we are not going any deeper in our path hierarchy beyond Data legacy.

If there happens to be additional directories under Data legacy, for instance Data legacy/Archive/set 1 then you would modify the endpoint to look like (see https://learn.microsoft.com/en-us/graph/api/driveitem-list-children?view=graph-rest-1.0&tabs=http#list-children-of-a-driveitem-with-a-known-path):

https://graph.microsoft.com/v1.0/drives/{drive ID}/root:/Data legacy/Archive/set 1:/children

Now that we know how to tell the Graph API the specific directory we are interested in, we still have not solved the problem of how to perform a sub-query within a specific path. If we search/sub-query at the root then we are able to find some results.

https://graph.microsoft.com/v1.0/drives/{drive ID}/root/search(q='a')

If we search on a sub-directory (as below) then we get nothing.

https://graph.microsoft.com/v1.0/drives/{drive ID}/root:/Data legacy/Archive/set 1:/children/search(q='a') 

Note from the MS documentation at https://learn.microsoft.com/en-us/graph/api/driveitem-search?view=graph-rest-1.0&tabs=http#http-request ALL searches seem to originate at root. Since we MUST use :/children after we specify a path in order to retrieve the child properties, Microsoft does need seem to have a way to perform a sub-query within a drive path other than root. :(

While we are not able to query a specific path within a drive, having the ability to narrow down a query to drive sub-directory could open up possible searching options outside of the Graph API (such as dumping all the results to a JSON file or database and then using a database query or Python search tool to achieve something similar).

Upvotes: 0

Sven
Sven

Reputation: 2505

You can also run the following request to search:

https://graph.microsoft.com/v1.0/me/drive/root:/path:/search(q='anything')

However, it seems that the results are not limited to this folder (path). I get results for anything in my drive.

Upvotes: 1

Guillaume R
Guillaume R

Reputation: 1

path = "fold1/subfold"

'https://graph.microsoft.com/v1.0/drives/' + driveId + '/root:/' + path + ":/search(q='')";

don't forget ":/" before "search"

Upvotes: 0

Related Questions