How to filter SharePoint sites by display name?

Stefank 41 Reputation points
2022-02-15T18:58:42.23+00:00

Hi,

I try to use Microsoft Graph API to get all SharePoint sites that have a display name that matches a certain pattern.

Using the /sites request and supplying a search-parameter allows this, of course. But when using wildcards sometimes search reports sites that do not seem to match the given search parameter at all.

What properties of a site are taken into account when using the search-parameter? Just the site's name / display name? Or other properties as well?

I tried using a filter-parameter but regardless of using v1.0 or beta graph API I always get the error that sites "cannot be enumerated".

Any idea how to use the search parameter on sites to have it just match the display name of sites?

Thanks!

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
11,809 questions
SharePoint Development
SharePoint Development
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Development: The process of researching, productizing, and refining new or existing technologies.
2,926 questions
0 comments No comments
{count} votes

Accepted answer
  1. Sreeju Nair 12,346 Reputation points
    2022-02-15T20:24:51.773+00:00

    As per the documentation states, The search filter is a free text search that uses multiple properties when retrieving the search results. So it fetches results by matching the keywords with the site properties.
    https://learn.microsoft.com/en-us/graph/api/site-search?view=graph-rest-1.0&tabs=http

    If you need more advanced search, you may use Search API, that have advanced capabilities. When you use Search API, you can use, entity type as Site, so the results contains only SharePoint sites. It supports KQL (Keyword Query Language) , so that you can build your advanced query

    https://learn.microsoft.com/en-us/graph/api/resources/search-api-overview?view=graph-rest-1.0#keyword-query-language-kql-support

    Hope this helps

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. AtulThorat-MSFT 391 Reputation points
    2022-02-15T21:44:42.94+00:00

    Hi @Stefank ,

    Hope you are doing well.

    Please refer below Microsoft article for filtering Sharepoint site.

    site-list (APIs under the /beta version in Microsoft Graph are subject to change. Use of these APIs in production applications is not supported.)

    Only the following query options are currently supported:

    ![174671-image.png

    GET /sites?$filter=siteCollection/root ne null

    GET https://graph.microsoft.com/v1.0/sites?$select=siteCollection,webUrl&$filter=siteCollection/root ne null

    Response :

    {  
        "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#sites(siteCollection,webUrl)",  
        "value": [  
            {  
                "webUrl": "https://raiinfy.sharepoint.com/",  
                "siteCollection": {  
                    "hostname": "raiinfy.sharepoint.com",  
                    "root": {}  
                }  
            }  
        ]  
    }  
    

    We can also used Search API to search any sharepoint site using below graph API endpoint.

    GET https://graph.microsoft.com/v1.0/sites?search=displayName
    GET https://graph.microsoft.com/v1.0/sites?search=Communication site

    Response :

    {  
        "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#sites",  
        "value": [  
            {  
                "createdDateTime": "2020-12-15T07:34:59Z",  
                "id": "raiinfy.sharepoint.com,e3a2252f-d91d-4c03-a670-682e77bbe8ba,2eb77667-cf78-4295-8130-333b77d4807a",  
                "lastModifiedDateTime": "2020-12-05T20:24:21Z",  
                "name": "raiinfy.sharepoint.com",  
                "webUrl": "https://raiinfy.sharepoint.com",  
                "displayName": "Communication site",  
                "root": {},  
                "siteCollection": {  
                    "hostname": "raiinfy.sharepoint.com"  
                }  
            }  
        ]  
    }  
    

    POST https://graph.microsoft.com/v1.0/search/query

    Body

    {  
        "requests": [  
            {  
                "entityTypes": [  
                    "site"  
                ],  
                "query": {  
                    "queryString": "Communication site"  
                }  
            }  
        ]  
    }  
    

    Response :

    {  
        "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#Collection(microsoft.graph.searchResponse)",  
        "value": [  
            {  
                "searchTerms": [  
                    "communication",  
                    "site"  
                ],  
                "hitsContainers": [  
                    {  
                        "total": 2,  
                        "moreResultsAvailable": false,  
                        "hits": [  
                            {  
                                "hitId": "raiinfy.sharepoint.com,e3a2252f-d91d-4c03-a670-682e77bbe8ba,2eb77667-cf78-4295-8130-333b77d4807a",  
                                "rank": 1,  
                                "summary": "<c0>Site</c0> Home 1 153 {7CC3543F<ddd/>",  
                                "resource": {  
                                    "@odata.type": "#microsoft.graph.site",  
                                    "displayName": "Communication site",  
                                    "id": "raiinfy.sharepoint.com,e3a2252f-d91d-4c03-a670-682e77bbe8ba,2eb77667-cf78-4295-8130-333b77d4807a",  
                                    "createdDateTime": "2020-12-15T07:34:59Z",  
                                    "lastModifiedDateTime": "2020-12-05T20:24:21Z",  
                                    "name": "raiinfy.sharepoint.com",  
                                    "webUrl": "https://raiinfy.sharepoint.com"  
                                }  
                            }  
                        ]  
                    }  
                ]  
            }  
        ]  
    }  
    

    Hope this helps.

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    0 comments No comments

  2. Stefank 41 Reputation points
    2022-02-15T22:30:01.573+00:00

    Thanks for the replies.

    As mentioned using Search filter does not work reliably for my use case as search takes several properties into account when doing a keyword match. For example, this request will return all sites not only the ones that have "site" in their name:

    https://graph.microsoft.com/beta/sites?$search="*site*"
    

    I guess this is caused because the WebUrl-property of a Site is matched as well. Thus, any WebUrl contains the word "site" this search will always return all sites.

    Doing this request:

    https://graph.microsoft.com/beta/sites?$search="*website*"
    

    I'd expect to get two sites returned which have a matching displayName: "App-Katalogwebsite" and "PointPublishing-Hubwebsite"
    But actually the request above only returns the "App-Katalogwebsite"

    To me this is completely intransparent against what properties Search-filter will do a match.

    But I appreciate your hint about the search API. Will give that a try,

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.