调用MS Graph API的/sites?search=接口无结果,但可获取根站点详情
sites?search= Endpoint Let’s figure out why you’re getting an empty value array when calling https://graph.microsoft.com/v1.0/sites?search= but can successfully retrieve the root site using /sites/root. Here are the most likely issues and fixes:
Possible Causes & Solutions
1. You’re Not Passing a Search Term
The search parameter needs a non-empty value to tell Graph what sites to look for. When you call sites?search= (with nothing after the equals sign), it doesn’t have any criteria to filter sites, so it returns an empty set.
Fix:
Add a relevant search term—like a site name, display name, or part of its URL. For example:
GET https://graph.microsoft.com/v1.0/sites?search=Contoso
2. Permissions Might Be Too Restricted
Even though you can access the root site, the site search endpoint might require slightly broader permissions. The root site is accessible with permissions like Sites.Read.All or Sites.ReadWrite.All, but search could need additional scope (especially if you’re trying to find sites outside the root).
Check & Fix:
- Confirm your app has the right delegated or application permissions. At minimum, you’ll need
Sites.Read.All(for delegated access) orSites.Read.All(for application access). - If using delegated permissions, make sure the signed-in user has access to the sites you’re trying to search—private sites won’t show up if the user doesn’t have permission to view them.
3. Sites Aren’t Indexed Yet (or Are Excluded from Search)
Newly created sites might take a few hours to be indexed by Microsoft Graph’s search engine. Also, some sites could be set to exclude themselves from search results.
Fix:
- Wait a few hours if the site was recently created—indexing doesn’t happen instantly.
- For SharePoint sites, check the site settings: Go to Site Settings > Search and offline availability and make sure "Allow this site to appear in search results?" is set to Yes.
4. Double-Check Your Endpoint Syntax
Make sure there are no typos or extra spaces in your request URL. The correct format for site search is:
GET https://graph.microsoft.com/v1.0/sites?search={yourSearchTerm}
(Note: Your example shows a space in the @odata.context response—ensure your actual request URL doesn’t have unintended spaces, as that can cause unexpected behavior.)
Test with a Known Site
To confirm everything works, try searching for your root site’s name. Since you can access /sites/root, use its display name in the search parameter. For example, if your root site is named "Contoso Portal", call:
GET https://graph.microsoft.com/v1.0/sites?search=Contoso%20Portal
This should return the root site in the value array if all permissions and settings are correct.
内容的提问来源于stack exchange,提问作者Dilip Narendra

