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

sharepoint 2013 rest api get all site collections
sharepoint rest api
sharepoint 2013 enable rest api
how rest api works in sharepoint 2013
sharepoint rest api get list items
rest api architecture in sharepoint 2013
sharepoint 2013 rest api javascript example
sharepoint rest api get all lists

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:

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.

Retrieve all sites and all subsites from the SharePoint REST API , You should add a path filter in the endpoint. Normal site collections have a path like https://tenant.sharepoint.com whereas personal (One  Master Team Sites, Lists, Documents, Templates & More - Start Today!

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

SharePoint Get all sites and all sub sites using SharePoint online , Get All Sites and Sub Sites using REST API. //The First Ajax is to get the Root Site Title and the Relative URL. // The Second AJAX is to get all the sub sites under the Root Site. // This is a Recursive Function to loop through the sub sites and check for more sub sites. SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It only takes a minute to sign up. Retrieve all sites and all subsites from the SharePoint REST API. Using the SharePoint Online REST API, I'm trying to retrieve a list of all sites and subsites.

https://yoursharepointsite.com/_api/search/query?querytext='(contentclass:STS_Site) (contentclass:STS_Web)'&trimduplicates=false&rowlimit=5000&selectproperties='Title,Url,Path,ParentLink'"

The above rest url shoul give you all the Sites and subsite the user has access to. You may need to trim duplicates.

Get All Sites and Sub Sites using REST API, Get Sub Sites in a SharePoint Site 2013 Using REST API. Lakshmanan Note: The following code is tested in my SP 2013 online environment. Step 1: Before So I have given full permission to all the contents listed below. Short blog post for today is about getting all your subsites & sub-subsites for a specific Site Collection within your SharePoint Online environment. As an admin for your Office 365 tenant, you may want to retrieve this information to pass on to an Executive in your

Get Sub Sites in a SharePoint Site 2013 Using REST API, Shows Lists, Libraries, Apps from the current site. REST API - https://ktskumar.​sharepoint.com/_api/web/AppTiles. Subsites. Shows subsites from  Welcome to the SharePoint 2013 REST Series. In my previous article, we saw how to get a list of site templates in a SharePoint Site using the REST API. This article explains how to retrieve a sub site for a given site using the REST API. The SharePoint 2013 environment adds the ability for you to remotely interact with SharePoint sites using REST.

Easiest Way To Get Site Contents And Subsites Using REST API, The endpoints in the SharePoint REST service correspond to the types For example, to retrieve all the lists in a specific SharePoint site, you  This SharePoint CSOM tutorial, we will discuss how to get all subsites from a site collection in SharePoint Online using .net managed object model (csom). Also, we will see how to get all subsites under a site collection using PowerShell in SharePoint Online Office 365. We will see how to retrieve all sites excluding the apps or add-in websites

Complete basic operations using SharePoint REST endpoints , Basics of using the SharePoint REST service to access and update SharePoint data, Retrieves all lists on a site, lists, GET, Not applicable The SharePoint Online (and on-premises SharePoint 2016 or later) REST service  Re: Getting the List of all sites/ sub-sites and their owners using PnP Did you try using Search webpart or Search REST API You can use search with either the Search Web Parts or using REST for search.

Comments
  • 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
  • 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
  • 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
  • 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