Retrieve User News Feed in SharePoint 2013 Using REST API

Introduction

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

Here I explain how to retrieve the newsfeed followed by the current user in SharePoint 2013 using the REST API and displaying it in the SharePoint page in the app model.

Procedure

  • On your Developer Site, open the "Napa" Office 365 Development Tools and then choose Add New Project.
  • Choose the App for the SharePoint template, name the project NewsFeed and then choose the Create button.

Prerequisites

These are the important steps that precede the creation of the APP.

Specify the following permissions that your app needs:

  • Choose the Properties button at the bottom of the page.
  • In the Properties window, choose Permissions.
  • In the Content category, set Write permissions for the Tenant scope.
  • In the Social category, set Read permissions for the User Profiles scope.
  • Close the Properties window.

Expand the Scripts node, choose the App.js file and delete the contents of the file and replace it with the following code:

  1. // In the App.js file, declare a global variable for the URL of the SocialFeedManager endpoint.  
  2. var feedManagerEndpoint;  
  3.   
  4. // Get the SPAppWebUrl parameter from the query string and build  
  5. // the feed manager endpoint.  
  6.   
  7. $(document).ready(function () {  
  8.     var appweburl;  
  9.     var params = document.URL.split("?")[1].split("&");  
  10.     for (var i = 0; i < params.length; i = i + 1) {  
  11.         var param = params[i].split("=");  
  12.         if (param[0] === "SPAppWebUrl") appweburl = param[1];  
  13.     }  
  14.     feedManagerEndpoint = decodeURIComponent(appweburl)+ "/_api/social.feed";  
  15.     postToMyFeed();  
  16. });  
  17.   
  18. // Publish a post to the current user's feed by using the   
  19. // "<app web URL>/_api/social.feed/my/Feed/Post" endpoint.  
  20.   
  21. function postToMyFeed() {  
  22.     $.ajax( {  
  23.         url: feedManagerEndpoint + "/my/Feed/Post",  
  24.         type: "POST",  
  25.         data: JSON.stringify( {   
  26.             'restCreationData':{  
  27.                 '__metadata':{   
  28.                     'type':'SP.Social.SocialRestPostCreationData'  
  29.                 },  
  30.                 'ID':null,   
  31.                 'creationData':{   
  32.                     '__metadata':{   
  33.                         'type':'SP.Social.SocialPostCreationData'  
  34.                     },  
  35.                 'ContentText':'This post was published using REST.',  
  36.                 'UpdateStatusText':false  
  37.                 }   
  38.             }   
  39.         }),  
  40.         headers: {   
  41.             "accept""application/json;odata=verbose",  
  42.             "content-type":"application/json;odata=verbose",  
  43.             "X-RequestDigest": $("#__REQUESTDIGEST").val()  
  44.         },  
  45.         success: getMyFeed,  
  46.         error: function (xhr, ajaxOptions, thrownError) {   
  47.             alert("POST error:\n" + xhr.status + "\n" + thrownError);  
  48.         }  
  49.     });  
  50. }  
  51.   
  52. // Get the current user's feed by using the   
  53. // "<app web URL>/_api/social.feed/my/Feed" endpoint.  
  54.   
  55. function getMyFeed() {  
  56.     $.ajax( {  
  57.         url: feedManagerEndpoint + "/my/Feed",  
  58.         headers: {   
  59.             "accept""application/json;odata=verbose"  
  60.         },  
  61.         success: feedRetrieved,  
  62.         error: function (xhr, ajaxOptions, thrownError) {   
  63.             alert("GET error:\n" + xhr.status + "\n" + thrownError);  
  64.         }  
  65.     });      
  66. }  
  67.   
  68. // Parse the JSON data and iterate through the feed.  
  69.   
  70. function feedRetrieved(data) {  
  71.     var stringData = JSON.stringify(data);  
  72.     var jsonObject = JSON.parse(stringData);   
  73.     var feed = jsonObject.d.SocialFeed.Threads;   
  74.     var threads = feed.results;  
  75.     var newscontent = "";  
  76.     for (var i = 0; i < threads.length; i++) {  
  77.         var thread = threads[i];  
  78.         var participants = thread.Actors;  
  79.         var owner = participants.results[thread.OwnerIndex].Name;  
  80.         newscontent += '<p>' + owner +   
  81.             ' said "' + thread.RootPost.Text + '"</p>';  
  82.     }    
  83.     $("#message").html(newscontent);   
  84. }  

Once you completed the code part then run the project.



After deploying the app select the Click here to launch your app in new window link and it will redirect to another page.



Click the Trust it button here.



Output

The current users followed news from my site. 



I hope you have enjoyed this.

Next Recommended Readings