How To Validate If Members Can Share The SharePoint Site Using Rest API

Each SharePoint site is associated with three SharePoint groups: Owners, Members, and Visitors. And the SharePoint site is also enabled to share with other users or external users.
 
Due to security reasons, sharing is mostly disabled for anyone other than owners. In this post, we are going to validate whether users who have member permission can have access to share the site or not.
 
The below REST API format will help to validate that,
 
https://domain.sharepoint.com/_api/web?$select=MembersCanShare
 
The response value returns true if the members have access to share the SharePoint Site and its folders/files. It returns false when members don’t have that permission.
 
The Sharing permissions can be managed from the SharePoint Site Permissions panel.
 
To test this using REST API in the browser instantly, navigate to the SharePoint site. Then open the browser’s web console and paste the below snippet,
  1. //getRequest method reference    
  2. //https://gist.github.com/ktskumar/a9e9df497673e9fd26ead8532b9ff425    
  3. function getRequest(url) {    
  4.     var request = new XMLHttpRequest();    
  5.     return new Promise(function(resolve, reject) {    
  6.         request.onreadystatechange = function() {    
  7.             if (request.readyState !== 4) return;    
  8.             if (request.status >= 200 && request.status < 300) {    
  9.                 resolve(request);    
  10.             } else {    
  11.                 reject({    
  12.                     status: request.status,    
  13.                     statusText: request.statusText    
  14.                 });    
  15.             }    
  16.         };    
  17.     
  18.         request.open('GET', url, true);    
  19.         request.setRequestHeader("Content-Type""application/json;charset=utf-8");    
  20.         request.setRequestHeader("ACCEPT""application/json; odata.metadata=minimal");           
  21.         request.setRequestHeader("ODATA-VERSION""4.0");    
  22.         request.send();    
  23.     
  24.     });    
  25. }   
  26.   
  27. //Modify domain.sharepoint.com with your SharePoint site URL  
  28. getRequest("https://domain.sharepoint.com/_api/web?$select=MembersCanShare").then(function(output) {    
  29.     var result = JSON.parse(output.response);  
  30.     if (result.MembersCanShare){    
  31.       alert("Members can share this site and its folder & files");    
  32.     }else{    
  33.       alert("Members do not have permission to share this site!");    
  34.     }        
  35. });  
  36. //Reference: https://gist.github.com/ktskumar/7cd6c870b67bfcef655003bf14d8bfca