How to Check if the Current User Follows the SharePoint Site

Introduction 

 
SharePoint Site has been enabled with a new type of social feature. You can now follow the site. If you started following the site, you will be up to date on the site updates. We can benefit from a lot of advantages, such as saving/copying/moving documents or items more easily, viewing recent items from the site, etc.
 
In this post, I’ll show you how to check whether the current user is following the SharePoint Site or not. The below REST API format will help to identify this;
 
https://domain.sharepoint.com/_vti_bin/homeapi.ashx/sites/followed/isFollowed?url=https://domain.sharepoint.com/sites/sitename
 
URL - This is the required parameter. You must pass this parameter to check the user is following the site associated with the URL. This enables us to run the code from any SharePoint site.
 
This returns the result in a Boolean value. If the current user follows the site, the result would be true. And the user doesn’t follow, it returns false.
 
To test this, navigate to any SharePoint Site. Then open the browser’s web console and paste the below code 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. function isFollow(url){  
  28. getRequest(location.origin+"/_vti_bin/homeapi.ashx/sites/followed/isFollowed?url="+url).then(function(output){console.log(output.response); });  
  29. }  
  30.   
  31. isFollow("https://contoso.sharepoint.com/sites/demo");  
  32. //https://gist.github.com/ktskumar/8a45f0c118ee2b6f5fb4ddcc6774fde2