Search code examples
sharepointsharepoint-onlinesharepoint-rest-api

SharePoint Get all sites and all sub sites using SharePoint online REST API


For SharePoint Online connector We used following steps to fetch all sites:

Step 1: Created Add-in on SharePoint instance with following permission xml

<AppPermissionRequests>
        <AppPermissionRequest Scope="http://sharepoint/content/tenant" Right="FullControl"/>
        <AppPermissionRequest Scope="http://sharepoint/content/sitecollection" Right="Read"/>
</AppPermissionRequests>

Step 2: Used below API to get all sites and subsites

https://<site_name>.sharepoint.com/_api/search/query?querytext='contentclass:STS_Site' &rowlimit=100

Issue we are facing

  1. Above endpoint is returning all sites, sub sites along with user’s personal site(One drive), while we need all sites and sub sites only.
  2. Please suggest minimal required permission to read all site, all subsite, all folders and files metadata

We referred following links:


Solution

  • You should add a path filter in the endpoint.

    Normal site collections have a path like https://tenant.sharepoint.com whereas personal (One Drive) site collections have path like https://tenant-my.sharepoint.com/personal/*.

    So, modify your endpoint as below:

    https://<site_name>.sharepoint.com/_api/search/query?querytext='contentclass:STS_Site
    Path:"https://<site_name>.sharepoint.com/*"'&rowlimit=500
    

    This will only return site collections starting with https://<site_name>.sharepoint.com path and will exclude the One Drive site collections which are on a different path.