Microsoft Graph中SharePoint搜索结果不一致及跨站点搜索方案咨询
Let's break this down clearly for you:
Is this expected behavior?
Yes, this is completely expected. The /me/drive/search endpoint is designed to only search within the current user's personal OneDrive—it does not include content from SharePoint sites, even if the user has access to them.
The wording in the Microsoft Graph docs about "items accessible to the user" can be misleading here; in this specific endpoint's context, it refers to items within the user's own OneDrive (including shared items stored in their OneDrive, but not items stored in SharePoint sites they can access).
Can you search all SharePoint sites the user has access to?
Absolutely, but you'll need to use a different endpoint instead of /me/drive/search:
Option 1: Use the unified search endpoint (recommended)
The /search/query endpoint is built for cross-scope searches across OneDrive and all SharePoint sites the user can access. Here's an example request:
POST https://graph.microsoft.com/v1.0/search/query Content-Type: application/json { "requests": [ { "entityTypes": [ "driveItem" ], "query": { "queryString": "brisbane" } } ] }
This will return all driveItem matches from both the user's OneDrive and any SharePoint sites they have permission to access.
Option 2: Iterate over user-accessible SharePoint sites (not recommended)
If you must use drive-specific search endpoints, you'd need to:
- First fetch all SharePoint sites the user has access to using
GET https://graph.microsoft.com/v1.0/sites?search=*(or filter based on permissions if needed) - For each site, call
/sites/{site-id}/drive/root/search(q='brisbane')and aggregate the results
This approach is inefficient and not scalable, so the unified /search/query is always the better choice.
Key Notes
- Make sure your application has the necessary permissions: For unified search, you'll need permissions like
Files.Read.AllandSites.Read.All(depending on the scope of content you need to access). - Your initial test results align perfectly with the endpoint's intended behavior: the site-specific drive search returns results from that SharePoint site, while
/me/drive/searchonly looks at the user's personal OneDrive.
内容的提问来源于stack exchange,提问作者user9842538

