Retrieve Followed Sites in SharePoint 2013 Using REST API

We can use the SharePoint 2013 Representational State Transfer (REST) service to do the same tasks you can do when you use the .NetCSOM, JSOM.

Here I explain how to retrieve the site name and URL followed by the current user in SharePoint 2013 using a client object model (REST API and JavaScript) and displaying it in the SharePoint page.

1. Create a new page and a Content Editor Webpart (CEWP).

Create a New page

2. Edit the web part that was added to the page.

Edit the web part

3. Upload your text file script into the site assests and copy the path of the text file and paste it into the Content link in CEWP.

4. Output

Output

Code

The following example shows how to retrieve all of the following sites:

  1. <html>    
  2.    <head>    
  3.       <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>    
  4.   
  5.   
  6.          <script type="text/javascript">  
  7.             var followingManagerEndpoint;  
  8.             var followedCount;  
  9.   
  10.             var followingEndpoint;    
  11.             var URL;    
  12.             var website;    
  13.             var clientContext;   
  14.    
  15.             SP.SOD.executeFunc('sp.js''SP.ClientContext', loadWebsite);    
  16.             function loadWebsite() {    
  17.                clientContext = SP.ClientContext.get_current();    
  18.                website = clientContext.get_web();    
  19.                clientContext.load(website);    
  20.                clientContext.executeQueryAsync(onRequestSucceeded, onRequestFailed);    
  21.             }    
  22.   
  23.             function onRequestSucceeded() {   
  24.   
  25.   
  26.               URL = website.get_url();    
  27.               followingManagerEndpoint = decodeURIComponent(URL) + "/_api/social.following";   
  28.    
  29.               getMyFollowedContent();  
  30.            }    
  31.     
  32.            function onRequestFailed(sender, args) {    
  33.              alert('Error: ' + args.get_message());    
  34.            }   
  35.   
  36.            // Get the content that the current user is following.  
  37.            // The "types=14" parameter specifies all content types  
  38.            // (documents = 2 + sites = 4 + tags = 8).  
  39.           function getMyFollowedContent() {  
  40.   
  41.             $.ajax( {  
  42.                  url: followingManagerEndpoint + "/my/followed(types=14)",  
  43.                  headers: {   
  44.                      "accept""application/json;odata=verbose"  
  45.                  },  
  46.                  success: followedContentRetrieved,  
  47.                  error: requestFailed  
  48.            });  
  49.         }  
  50.   
  51.         // Parse the JSON data and iterate through the collection.  
  52.   
  53.   
  54.         function followedContentRetrieved(data) {  
  55.             var stringData = JSON.stringify(data);  
  56.             var jsonObject = JSON.parse(stringData);   
  57.             var types = {  
  58.               1: "document",  
  59.               2: "site",  
  60.               3: "tag"   
  61.            };  
  62.    
  63.            var followedActors = jsonObject.d.Followed.results;   
  64.            var followedList = "You're following items:";  
  65.   
  66.            for (var i = 0; i < followedActors.length; i++) {  
  67.               var actor = followedActors[i];  
  68.               followedList += "<p>The " + types[actor.ActorType] + ": \"" +actor.Name + "\"</p>"+"<p>Site URL " + ": \"" +  
  69.               actor.Uri+ "\"</p>";;  
  70.            }   
  71.            $("#Follow").html(followedList);   
  72.         }  
  73.   
  74.         function requestFailed(xhr, ajaxOptions, thrownError) {  
  75.           alert('Error:\n' + xhr.status + '\n' + thrownError + '\n' + xhr.responseText);  
  76.         }  
  77.   
  78.       </script>    
  79.    </head>    
  80. <body>    
  81. <div id="Follow"></div>    
  82. </body>    
  83. </html>