Get My Followers From SharePoint 2013 Using REST API

Introduction

This article helps to retrieve all the users following the current user 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:

  • Tenant = Write
  • User Profiles = Read

Step 3

Update the Default.aspx and App.js files

Default ASPX



App.js

  1. 'use strict';  
  2.   
  3. var hostweburl;  
  4. var appweburl;  
  5. var myFollowerEndpoint;  
  6. var followers;  
  7.   
  8. $(document).ready(function(){  
  9.     hostweburl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));  
  10.     appweburl = decodeURIComponent(getQueryStringParameter("SPAppWebUrl"))  
  11.       
  12.     myFollowerEndpoint = decodeURIComponent(appweburl) + "/_api/social.following";  
  13.     getMyFollowers();  
  14. });  
  15.   
  16. function getQueryStringParameter(paramToRetrieve)  
  17. {  
  18.     var params = document.URL.split("?")[1].split("&");  
  19.     for (var i=0; i<params.length; i= i + 1)  
  20.     {  
  21.         var singleParam = params[i].split("=");  
  22.         if(singleParam[0] == paramToRetrieve) return singleParam[1];          
  23.     }  
  24. }  
  25.   
  26. function getMyFollowers() {   
  27.     $.ajax( {  
  28.         url: myFollowerEndpoint + "/my/followers",  
  29.         headers: {   
  30.             "accept""application/json;odata=verbose"  
  31.         },  
  32.         success: myFollowersSuccessHandler,  
  33.         error: myFollowersErrorHandler  
  34.     });  
  35. }  
  36.   
  37. function myFollowersSuccessHandler(data)  
  38. {  
  39.     var stringData = JSON.stringify(data);  
  40.     var jsonObject = JSON.parse(stringData);   
  41.       
  42.     var folResult = jsonObject.d.Followers.results;  
  43.     followers = "<p>The Person who follows you are:</p>";   
  44.     for (var i=0; i<folResult.length; i++)  
  45.     {  
  46.         followers+= "<P>"+folResult[i].Name+"</p>";  
  47.     }   
  48.       
  49.     document.getElementById("myFollowersResult").innerHTML = followers;  
  50. }  
  51.   
  52. function myFollowersErrorHandler(data,errorcode,errormessage)  
  53. {  
  54.     alert("Couldn't get the followers " + errormessage);  
  55. }  
Step 4

Publish the solution and click the Trust It Button.

Output