How to count sharepoint sites

Maxime Clavel 0 Reputation points
2023-10-05T16:30:12.88+00:00

Here is our current code in Go (GoLang) using the msgraph-sdk-go: pkg:

func (me *graphClient) SitesCount(ctx context.Context) (int64, error) {
 	siteRequester := me.graphSdkClient.Sites()

  	headers := abstractions.RequestHeaders{}
 	headers.Add("ConsistencyLevel", "eventual")

  	displayCount := true
 	top := int32(0)

  	config := &sites.SitesRequestBuilderGetRequestConfiguration{
 		QueryParameters: &sites.SitesRequestBuilderGetQueryParameters{
 			Count: &displayCount,
 			Top:   &top,
 		},
 		Headers: &headers,
 	}

  	req, err := siteRequester.Get(ctx, config)
 	if err != nil {
 		return 0, err
 	}

  	totalCount := req.GetOdataCount()
  	return *totalCount, nil
 }

HTTP API call:

GET https://graph.microsoft.com/v1.0/sites?$count=true&$top=0

This returns the count of sites (SharePoint and personal/OneDrive).

We would like to get the count of SharePoint sites only (without personal/OneDrive).

We saw the additionalData field where isPersonal=true. Can we use this field? If yes, how?

Summary: How can we get the count of SharePoint sites in one call without iterating on every sites?

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

1 answer

Sort by: Most helpful
  1. RaytheonXie_MSFT 36,576 Reputation points Microsoft Vendor
    2023-10-06T03:12:51.6933333+00:00

    Hi @Maxime Clavel,

    Per my research, currently we are unbale to filter the site by isPersonal properties. As a workaround, I would recommend you to use graph search to get the sharepoint site. Since the url of OneDrive starts with https://contoso-my.sharepoint.com/, and the url of SharePoint site starts with https://contoso.sharepoint.com/. So you could add filter in th query like this to ignore Onedrive site:

    "queryString":"path:\"https://contoso.sharepoint.com/*""
    

    Reference:

    https://learn.microsoft.com/en-us/graph/search-concept-files#example-5-use-filters-in-search-queries


    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.


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.