Retrieve Followed Content From SharePoint 2013 Using REST

Introduction

This article helps to retrieve the followed content from MySite using REST in SharePoint 2013. This is developed using the NAPA development tool.

Step 1

On your Developer Site, open the "Napa" Office 365 Development Tools and then choose Add New Project.

  • Choose the App for SharePoint template, name the project and then click the Create button
  • Replace Default.aspx with the following code.
  • Replace APP.js with the following source code.
  • Publish Your App.

Step 2

Change the Permission as in the following:

Tenant = Write

User Profiles = Read



Step 3

Update the Default.aspx and App.js files.

Default ASPX



App.js

 

  1. 'use strict';  
  2. var hostweburl;  
  3. var appweburl;  
  4. var followContentEndpoint;  
  5. $(document).ready(function()   
  6. {  
  7.     hostweburl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));  
  8.     appweburl = decodeURIComponent(getQueryStringParameter("SPAppWebUrl"))  
  9.   
  10.     followContentEndpoint = decodeURIComponent(appweburl) + "/_api/social.following";  
  11.     getMyFollowedContent();  
  12.     $.getScript(hostweburl + "/_layouts/15/SP.RequestExecutor.js");  
  13. });  
  14. function getQueryStringParameter(paramToRetrieve)  
  15. {  
  16.     var params = document.URL.split("?")[1].split("&");  
  17.     for (var i = 0; i < params.length; i = i + 1)   
  18.     {  
  19.         var singleParam = params[i].split("=");  
  20.         if (singleParam[0] == paramToRetrieve) return singleParam[1];  
  21.     }  
  22. }  
  23. function getMyFollowedContent()  
  24. {  
  25.     $.ajax({  
  26.         url: followContentEndpoint + "/my/followed(types=14)",  
  27.         headers: {  
  28.             "accept""application/json;odata=verbose"  
  29.         },  
  30.         success: followedContentRetrieved,  
  31.         error: followContentErrorHandler  
  32.     });  
  33. }  
  34. function followedContentRetrieved(data)   
  35. {  
  36.     var stringData = JSON.stringify(data);  
  37.     var jsonObject = JSON.parse(stringData);  
  38.     var types = {  
  39.         1: "document",  
  40.         2: "site",  
  41.         3: "tag"  
  42.     };  
  43.     var followedActors = jsonObject.d.Followed.results;  
  44.     var followedList = "You're Following the below contents:";  
  45.     for (var i = 0; i < followedActors.length; i++) {  
  46.         var actor = followedActors[i];  
  47.         followedList += "<p>" + types[actor.ActorType] + ": \"" + actor.Name + "\"</p>";  
  48.     }  
  49.     $("#fldCntResult").html(followedList);  
  50. }  
  51. function followContentErrorHandler(data, errorcode, errormessage)   
  52. {  
  53.     alert("Couldn't generate the Count" + errormessage);  
  54. }  

 

Step 4

Publish the solution and click the Trust It Button.



Output