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

Prerequisites

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

Specify the following permissions that your app needs:

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

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.