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

10,106

Solution 1

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.

Solution 2

A way from Joel Dsouza for your reference.

1.The First Ajax is to get the Root Site Title and the Relative URL.

$.ajax({
    url: _spPageContextInfo.siteAbsoluteUrl + "/_api/site/rootweb?$select=Title,ServerRelativeUrl",
    method: "GET",
    headers: {
        "Accept": "application/json; odata=verbose"
    },
    success: function(rootsite) {

    },
    error: function(rootsite) {},
    async: false
});

2.The Second AJAX is to get all the sub sites under the Root Site.

$.ajax({
    url: _spPageContextInfo.siteAbsoluteUrl + "/_api/web/webinfos?$select=ServerRelativeUrl,Title",
    method: "GET",
    headers: {
        "Accept": "application/json; odata=verbose"
    },
    success: function(subsites) {
        $.each(subsites.d.results, function() {
            getSubSites(this.ServerRelativeUrl, this.Title);
        });

    },
    error: function(subsites) {},
    async: false
});

3.This is a Recursive Function to loop through the sub sites and check for more sub sites.

function getSubSites(SubSiteUrl, SubSiteTitle) {
    console.log(SubSiteUrl);
    $.ajax({
        url: _spPageContextInfo.siteAbsoluteUrl + SubSiteUrl + "/_api/web/webinfos?$select=ServerRelativeUrl,Title",
        method: "GET",
        headers: {
            "Accept": "application/json; odata=verbose"
        },
        success: function(subsites) {

            $.each(subsites.d.results, function(index) {
                getSubSites(this.ServerRelativeUrl, this.Title);
            });
        },
        error: function(subsites) {},
        async: false
    });
}

More information: Get All Sites and Sub Sites using REST API

Share:
10,106
Darshan Patel
Author by

Darshan Patel

Don't waste a good mistake... Learn from it.

Updated on June 15, 2022

Comments

  • Darshan Patel
    Darshan Patel almost 2 years

    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:

  • Darshan Patel
    Darshan Patel almost 6 years
    I don't want to hit multiple API for that. As suggested by @gautam I am able to get expected result. Thanks for the answer
  • Darshan Patel
    Darshan Patel almost 6 years
    Thanks Gautam <AppPermissionRequest Scope="http://sharepoint/content/tenant" Right="FullControl"/> Could you please correct me on the Add-in permission part. I need to set read permission only. please check updated question Thanks
  • Gautam Sheth
    Gautam Sheth almost 6 years
    If its just for display/retrieval purposes, then read permission would be enough. But if you need to do more processing on it, then you will need higher permissions
  • Darshan Patel
    Darshan Patel almost 6 years
    yes, its only for /retrieval. I think following permissions would be sufficient. <AppPermissionRequest Scope="http://sharepoint/search" Right="QueryAsUserIgnoreAppPrincipal"/> <AppPermissionRequest Scope="http://sharepoint/content/sitecollection" Right="Read"/> Please correct me if I am wrong