reactjsmicrosoft-graph-apispfx

Getting nothing returned when using msGraph to get a list of sites from an SPO tenant


I'm attempting to use msGraph to get a list of all hubsites, sites, subsites from my SharePoint Online tenant. Here is the code I'm using:

Package-solution:

"webApiPermissionRequests": [
      {
        "resource": "Microsoft Graph",
        "scope": "User.ReadBasic.All"
      }
    ],

Data access:

export const GetSitesMsGraph = (context: any) => {
 return context.msGraphClientFactory
    .getClient('3')
    .then((client: MSGraphClientV3) => {
      client
        .api('sites')
        .version("v1.0")
        .get((err, res) => {
          if (err) {
            console.error(err);
            return;
          }
          console.log(res)
          return res;
        })
    })
}

This returns an empty array. I have tried swapping .api('sites') with .api('/sites') and still no return of data.

If I replace .api('sites') with .api('users') I get a full list of users from the tenant. Is this sites incorrect? I have made sure all SPFx calls that use msGraph have permission on the tenant admin.

I have read the below also:

https://learn.microsoft.com/en-us/sharepoint/dev/spfx/use-aad-tutorial


Solution

  • Yes, by default the Graph API endpoint /sites returns empty value.

    Workaround can be to use either search query parameter

    .api('sites?search=')
    .api('sites?search=*')
    

    or with application permissions, you can list all sites

    .api('sites/getAllSites')
    

    What I remember even /sites?search=* doesn't return all subsites (across all geographies)