How to get "Shared with me" items from OneDrive using Microsoft Graph Search API (I cooked this solution with the help of GitHub Copilot)
The Problem
I needed to programmatically retrieve all folders shared with me from other users' OneDrive (the same list you see in the "Shared with me" view in the browser).
The obvious endpoint /me/drive/sharedWithMe only returned 1 item, while the browser showed 6 folders. This API seems unreliable for OneDrive for Business.
The Solution
Use the Microsoft Graph Search API with a KQL (Keyword Query Language) filter:
POST
{
"requests": [{
"entityTypes": ["driveItem"],
"query": {
"queryString": "isDocument:false AND path:\"<other-users-onedrive-base-path>\" AND -path:\"<my-onedrive-path>\""
},
"from": 0,
"size": 500,
"fields": ["name", "createdBy", "parentReference", "webUrl", "id"]
}]
}
KQL Query Breakdown
isDocument:false → Returns only folders, excludes files
path:"<base-path>" → Filters to only personal OneDrive items (e.g., https://contoso-my.sharepoint.com/personal/)
-path:"<my-path>" → Excludes items from my own OneDrive
Key Findings
/me/drive/sharedWithMe is unreliable for OneDrive for Business - it doesn't return all shared items
Search API works perfectly - returns all items that match the criteria
KQL path: filter works with URL prefixes - you can filter by the OneDrive base URL to get only personal OneDrive items (excluding SharePoint sites)
Deduplication may be needed - Search API can return duplicates, so filter by item ID
Authentication
I used MSAL with ROPC (Resource Owner Password Credentials) flow with a service account. The app registration needs Files.ReadWrite.All and Sites.Read.All permissions.
Result
Now I can retrieve exactly the same 6 folders that I see in the browser's "Shared with me" view!
HTH