How to fetch the total site count in SharePoint using Graph API?

I’m trying to figure out a way to get the total number of SharePoint sites in our system using Microsoft Graph API. The problem is that the API doesn’t seem to support the $count parameter for the search endpoint. I’ve also tried querying sites directly with a count, but that didn’t work either.

Here are a couple of things I’ve attempted:

GET /v1.0/sites?search=*&$select=name&$top=999
GET /v1.0/sites?$count=true

Both of these requests failed to give me the total count. Is there any other way to get this information in a single API call? I’d really appreciate any suggestions or workarounds. Thanks in advance for your help!

hey there, i’ve run into this issue too! sadly, graph api doesn’t have a straightforward way to get the total site count. what i ended up doing was paginating through all sites and counting manually. it’s not ideal, but it works. you could also try using powershell if that’s an option for you. good luck!

As someone who’s dealt with this issue, I can tell you it’s a bit of a pain. The Graph API doesn’t make it easy to get a simple site count. What I’ve found to work is using the search endpoint with a wildcard and then implementing pagination. It’s not elegant, but it gets the job done.

Here’s a rough outline of what I did:

  1. Use the search endpoint with a wildcard: GET /v1.0/sites?search=*
  2. Implement pagination to go through all results
  3. Keep a counter and increment it for each site

It’s time-consuming for large tenants, but it’s reliable. If you’re dealing with a massive number of sites, you might want to look into batch requests to speed things up a bit.

Another trick I’ve used is leveraging the SharePoint REST API instead. Sometimes it’s faster for these types of operations. Just something to consider if Graph API is giving you headaches.

Unfortunately, there’s no direct method to fetch the total site count in a single Graph API call. As a workaround, you could implement pagination and iterate through the results, keeping a running count. This approach, while not ideal, is often the most reliable.

Another option is to utilize the SharePoint Search API, which might provide faster results for large tenants. You could construct a search query that returns all sites and extract the total count from the response metadata.

If you’re open to alternatives, consider using PowerShell with the PnP module or the SharePoint Online Management Shell. These tools often provide more straightforward methods for obtaining such metrics.

Ultimately, the best approach depends on your specific use case and the scale of your SharePoint environment.