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. $.ajax({
  3. "async": true,
  4. "crossDomain": true,
  5. "url": "https://cors-anywhere.herokuapp.com/https://login.microsoftonline.com/sharepointtechie.onmicrosoft.com/oauth2/v2.0/token", // Pass your tenant name instead of sharepointtechie
  6. "method": "POST",
  7. "headers": {
  8. "content-type": "application/x-www-form-urlencoded"
  9. },
  10. "data": {
  11. "grant_type": "client_credentials",
  12. "client_id ": "8baf0301-27df-44b1-b4fe-7911b9a918de", //Provide your app id
  13. "client_secret": "tZ76oVPN039WlWPoAp+1aICq66vs7oUtE4lhDQYwxGY=", //Provide your secret
  14. "scope ": "https://graph.microsoft.com/.default"
  15. },
  16. success: function(response) {
  17. console.log(response);
  18. token = response.access_token;
  19. console.log(token);
  20. getSiteInformation();
  21. },
  22. error: function(error) {
  23. console.log(JSON.stringify(error));
  24. }
  25. })
  26. }
Create a function "getSiteInformation" to initate the site collection request after successfully fetching the access token.

  1. function getSiteInformation(){
  2. $.ajax({
  3. "async": true,
  4. "crossDomain": true,
  5. "url": "https://graph.microsoft.com/v1.0/sites?search=*",
  6. "method": "GET",
  7. "headers": {
  8. "authorization": "Bearer" + token,
  9. "content-type": "application/json"
  10. },
  11. success: function(response) {
  12. console.log(response.value);
  13. },
  14. error: function(error) {
  15. console.log(JSON.stringify(error));
  16. }
  17. })
  18. }
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. $.ajax({
  3. method: 'GET',
  4. url: "https://graph.microsoft.com/v1.0/sites/sharepointtechie.sharepoint.com,1a634409-8e14-4d07-b1a7-366eec870233,4047d743-691c-4578-9795-4c1458096e98/sites",
  5. headers: {
  6. 'Authorization': 'Bearer ' + token,
  7. 'Content-Type': 'application/json'
  8. },
  9. }).success(function(response) {
  10. console.log(response.value);
  11. var data = response.value;
  12. for(var i=0; i<data.length; i++){
  13. console.log(data[i].displayName);
  14. var html = "<tr><td>" + data[i].displayName+ "</td><td>" + data[i].webUrl+ "</td><td>" + data[i].createdDateTime+ "</td>";
  15. $('.table tbody').append(html);
  16. }
  17. }).error(function(error) {});
  18. }
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.