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 –
sites, sub sites along with user’s
personal site(One drive)
, while we need all sites and sub sites
only.We referred following links:
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