Get My Followed Sites in SharePoint Online

In SharePoint 2013 new social features are incorporated, one of those is the following. We can follow Users / Peoples, Documents, Sites and Tags. This is a very good reference.

Scenario

We have our portal site collection and my site collection in Office 365 sites. We have a requirement to show only sites that are followed by the current user (the user who is currently logged into the site) on the portal site collection.

On my site collection this WebPart is already available as in the following screenshot:

OOB XSLTViewer web part
Figure 1: OOB XSLTViewer web part on MySite: Sites I'm following

On my site collection this is XSLTViewer web part is used for the Social list. For more details see how it is configured.

But we don't have the availability of the Social list on the portal site collection, so we can't use the web part that is on My site collection.

Approach: We decided to use the rest service for getting the current user followed sites as /_api/social.following/my/followed%28types=4%29
More details on Social feed REST API reference for SharePoint 2013. Here type is 4 that is for sites. %28 for "(" and 29 for ")". If you want to directly run into browser try like this:


<Site Collection>/_api/social.following/my/followed(types=4)

We have written one JavaScript file, called the preceding rest service and just appended in Div tag. We have uploaded the JavaScript file in “Master Page Gallery”.

We have added a content editor web part on the page and you can refer to the JavaScript link as:

Referring JS in Content Editor web part
Figure 2: Referring to JavaScript in Content Editor web part

Code snippet: (please bear with the UI and HTML formatting). I am also attaching the complete JavaScript file in code.
  1. //function : calls rest service  
  2. function getMyFollowedSites()   
  3. {  
  4. var restUrl = _spPageContextInfo.webAbsoluteUrl + "/_api/social.following/my/followed%28types=4%29"//Type 4 for sites  
  5.   
  6.     var executor = new SP.RequestExecutor(_spPageContextInfo.webAbsoluteUrl);  
  7.     executor.executeAsync(  
  8.     {  
  9.         url: restUrl,  
  10.         method: "GET",  
  11.         headers: { "Accept""application/json; odata=verbose" },  
  12.         success: successHandler,  
  13.         error: failureHandler  
  14.     });  
  15. }  
  16.   
  17. //success callback  
  18. function successHandler(data)   
  19. {  
  20.   var jsonObject = JSON.parse(data.body);  
  21.   var results = jsonObject.d.Followed.results;  
  22.   var htmlString = '';  
  23.    
  24.     // preparing the HTML for the result  
  25.     for (resultCount=0;resultCount<results.length;resultCount++)  
  26.     {  
  27.         //Preparing the HTML  
  28.     htmlString += "<a id=\"aid"+resultCount+"\"title=\""+results[resultCount].Name+"\" href=\""+results[resultCount].Uri+"\">"+results[resultCount].Name+"</a>      ";      
  29.   
  30.                     htmlString += results[resultCount].Uri;  
  31.                     htmlString += "<br/>";  
  32.     }  
  33.     htmlString +="</div>";  
  34.   
  35.     htmlResultDiv = document.getElementById('htmlResult');  
  36.     htmlResultDiv.innerHTML=htmlString;  
  37. }  
  38.    
  39.  // Failure callback  
  40. function failureHandler(data, errorCode, errorMessage)   
  41. {  
  42.    alert('Error Code: ' + errorCode);  
  43.    alert('Error Message: ' + errorMessage);  
  44. }  
HTML
  1. <div>  
  2.    <div id="htmlResult" style="float:left;"></div>  
  3. </div>  
Thanks!

Enjoy reading.

Feel free to comment/suggest/feedback.