Retrieve Site Collections And SubSites Using Microsoft Graph API

In this article, I have explained how to retrieve the top site collections and subsites using Microsoft Graph API. In my previous article, I have already written about how to fetch access token to authorize your web application with Microsoft Graph. Use the below link to learn about  Microsoft Graph,
Let's get started to retrieve SharePoint top site collections and subsites using Microsoft Graph API
 
Retrieve SharePoint Tenant Root Site
 
EndPoint - https://graph.microsoft.com/sites/root 
 
It returns the root site properties id, webUrl, displayName, CreateddateTime, LastModified etc...
 
Retrieve Site Collections And SubSites Using Microsoft Graph API
 
Retrieve Sharepoint Site Collection 
 
EndPoint  -  https://graph.microsoft.com/v1.0/sites?search=*
 
Here we are using search equals to "*" to retrieve all the top level site collections from the SharePoint tenant host.
 
Code 
  1. function requestToken() {  
  2.   
  3.         $.ajax({  
  4.             "async"true,  
  5.             "crossDomain"true,  
  6.             "url""https://cors-anywhere.herokuapp.com/https://login.microsoftonline.com/sharepointtechie.onmicrosoft.com/oauth2/v2.0/token", // Pass your tenant name instead of sharepointtechie    
  7.             "method""POST",  
  8.             "headers": {  
  9.                 "content-type""application/x-www-form-urlencoded"  
  10.             },  
  11.             "data": {  
  12.              "grant_type""client_credentials",    
  13.             "client_id ""8baf0301-27df-44b1-b4fe-7911b9a918de"//Provide your app id    
  14.             "client_secret""tZ76oVPN039WlWPoAp+1aICq66vs7oUtE4lhDQYwxGY="//Provide your secret    
  15.             "scope ""https://graph.microsoft.com/.default"  
  16.             },  
  17.             success: function(response) {  
  18.                 console.log(response);  
  19.                 token = response.access_token;  
  20.                 console.log(token);  
  21.                  getSiteInformation();  
  22.             },  
  23.             error: function(error) {  
  24.             console.log(JSON.stringify(error));  
  25.            }  
  26.         })  
  27.     }  
 Create a function "getSiteInformation" to initate the site collection request after successfully fetching the access token.

  1. function getSiteInformation(){  
  2.   
  3.   $.ajax({  
  4.   "async"true,  
  5.   "crossDomain"true,  
  6.   "url""https://graph.microsoft.com/v1.0/sites?search=*",  
  7.   "method""GET",  
  8.   "headers": {  
  9.     "authorization""Bearer" + token,  
  10.     "content-type""application/json"  
  11.   },  
  12.     
  13.          success: function(response) {  
  14.                console.log(response.value);  
  15.            
  16.             },  
  17.             error: function(error) {  
  18.             console.log(JSON.stringify(error));  
  19.            }  
  20. })  
  21.   
  22. }  
 After a successful response, it retrieves all 200 of my site collections from office 365 SharePoint.
 
Retrieve Site Collections And SubSites Using Microsoft Graph API
 
Let's append it into a simple table to show the details of site collections
 
Final Result
 
Retrieve Site Collections And SubSites Using Microsoft Graph API 
 
Retrieve Subsite Using Microsoft Graph
 
EndPoint - https://graph.microsoft.com/v1.0/sites/{{site collection id}}/sites
  
Code
  1. function getSubsite() {    
  2.   
  3.     $.ajax({    
  4.         method: 'GET',    
  5.         url: "https://graph.microsoft.com/v1.0/sites/sharepointtechie.sharepoint.com,1a634409-8e14-4d07-b1a7-366eec870233,4047d743-691c-4578-9795-4c1458096e98/sites",    
  6.         headers: {    
  7.             'Authorization''Bearer ' + token,    
  8.             'Content-Type''application/json'    
  9.         },    
  10.     }).success(function(response) {    
  11.         console.log(response.value);    
  12.         var data = response.value;  
  13.         for(var i=0; i<data.length; i++){  
  14.          console.log(data[i].displayName);  
  15.            
  16.          var html = "<tr><td>" + data[i].displayName+ "</td><td>" + data[i].webUrl+ "</td><td>" + data[i].createdDateTime+ "</td>";    
  17.          $('.table tbody').append(html);  
  18.         }  
  19.           
  20.           }).error(function(error) {});    
  21. }     
Response
 
Retrieve Site Collections And SubSites Using Microsoft Graph API 
 
Finally, we retrieved the subsites using Microsoft Graph API. 
 
Retrieve Site Collections And SubSites Using Microsoft Graph API 
 
So, this article will help you to retrieve the Sharepoint top-level site collection and subsite of the site collections.