How to search all sites with graph API

test-se 6 Reputation points
2022-06-27T07:42:45.21+00:00

Issue

I am trying to get a list of all sites in Sharepoint using the Grapsh API to see if there is a specific site whose site name starts with XXX.

But Graph API site search doesn't work as expected.

I performed a site search using https://graph.microsoft.com/v1.0/sites?search=*, but each time I run the API, the number of sites that are hit by the search changes. (1st 85,744 sites, 2nd 85,778 sites, 3rd 85,777 sites )

   # example  
     
    * Request URL : `https://graph.microsoft.com/v1.0/sites?search=*`  
     
    * Response Code : 200 (Since there is no error, the permission (Sites.Read.All) should have been granted.)  
     
   * Response Body (Resut is Paging)  
   {  
       "@odata.nextLink": "https://graph.microsoft.com/v1.0/sites?search=*&$skiptoken=s!xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",   
       "value":   
           {  
               "displayName": "DISPLAY_NAME_001",   
               "name": "XXX_001",   
               "webUrl": "https://host.sharepoint.com/sites/XXX_001",   
               "lastModifiedDateTime": "YYYY-MM-DDTHH:MM:SSZ",   
               "createdDateTime": "YYYY-MM-DDTHH:MM:SSZ",   
               "siteCollection": {  
                   "hostname": "host.sharepoint.com"  
               },   
               "root": {},   
               "id": "host.sharepoint.com,a0000000-0000-0000-0000-000000000001,b0000000-0000-0000-0000-000000000001"  
           },   
           {  
               "displayName": "DISPLAY_NAME_002",   
               "name": "XXX_002",   
               "webUrl": "https://host.sharepoint.com/sites/XXX_002",   
               "lastModifiedDateTime": "YYYY-MM-DDTHH:MM:SSZ",   
               "createdDateTime": "YYYY-MM-DDTHH:MM:SSZ",   
               "siteCollection": {  
                   "hostname": "host.sharepoint.com"  
               },   
               "root": {},   
               "id": "host.sharepoint.com,a0000000-0000-0000-0000-000000000002,b0000000-0000-0000-0000-000000000002"  
           }  
       ],   
       "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#sites"  
   }  
   ・  
   ・  
   ・  
   * Last page  
   {  
       "value": [],   
       "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#sites"  
   }  

Also, when I specify a site ID in the seach={query} when searching, there are cases where the search hits, and cases where the search does not hit, and the results differ depending on the target site.

   # example  
     
   ## hit case  
     
   * Request URL : `https://graph.microsoft.com/v1.0/sites?search=a0000000-0000-0000-0000-000000000001`  
     
   * Response Code : 200  
     
   * Response Body  
   {  
       "value":   
           {  
               "displayName": "DISPLAY_NAME_001",   
               "name": "XXX_001",   
               "webUrl": "https://host.sharepoint.com/sites/XXX_001",   
               "lastModifiedDateTime": "YYYY-MM-DDTHH:MM:SSZ",   
               "createdDateTime": "YYYY-MM-DDTHH:MM:SSZ",   
               "siteCollection": {  
                   "hostname": "host.sharepoint.com"  
               },   
               "root": {},   
               "id": "host.sharepoint.com,a0000000-0000-0000-0000-000000000001,b0000000-0000-0000-0000-000000000001"  
           },  
       ],   
       "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#sites"  
   }  
     
     
   ## no hit case  
   * Request URL : `https://graph.microsoft.com/v1.0/sites?search=a0000000-0000-0000-0000-000000000002`  
     
   * Response Code : 200  
     
   * Response Body  
   {  
       "value": [],   
       "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#sites"  
   }  

---

Question

I have two questions about this phenomenon.

1. Why do the search results change with each search? What does serach={query} search for on a SharePoint site? And how "query" should be structured?

I don't understand the specification of the query because the documentation does not clearly explain what the search={query} searches for and how the query is structured.

https://learn.microsoft.com/en-us/graph/api/site-search?view=graph-rest-1.0&tabs=http

2. Is there any way to search all sites in the Grapsh API other than search=*?

According to this URL , it states

   The `search=*` query may give you the results you're after but it is not guaranteed in all scenarios.     
     Graph API search may not support all site searches.  

If the search=* in the Grapsh API doesn't allow me to search all sites, is there any other way to get all sites?

---

I referred to the following sites but could not find a solution, so I asked the question.

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
10,715 questions
SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
9,738 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Zehui Yao_MSFT 5,831 Reputation points
    2022-06-29T07:40:41.637+00:00

    Hi @test-se , Regarding the use of the {query} parameter, only this part is mentioned in the documentation at present.
    At the same time, in the experience of most engineers, using search=* is currently the only way to get all sites.

    215997-untitled.png

    You can use this support link to submit feature request ideas to request updates to the documentation content.
    The link will be monitored by the Microsoft team and enhanced with the Microsoft Graph API. I will also vote for you.
    Hope it can help you.


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.