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:
- // In the App.js file, declare a global variable for the URL of the SocialFeedManager endpoint.
- var feedManagerEndpoint;
- // Get the SPAppWebUrl parameter from the query string and build
- // the feed manager endpoint.
- $(document).ready(function () {
- var appweburl;
- var params = document.URL.split("?")[1].split("&");
- for (var i = 0; i < params.length; i = i + 1) {
- var param = params[i].split("=");
- if (param[0] === "SPAppWebUrl") appweburl = param[1];
- }
- feedManagerEndpoint = decodeURIComponent(appweburl)+ "/_api/social.feed";
- postToMyFeed();
- });
- // Publish a post to the current user's feed by using the
- // "<app web URL>/_api/social.feed/my/Feed/Post" endpoint.
- function postToMyFeed() {
- $.ajax( {
- url: feedManagerEndpoint + "/my/Feed/Post",
- type: "POST",
- data: JSON.stringify( {
- 'restCreationData':{
- '__metadata':{
- 'type':'SP.Social.SocialRestPostCreationData'
- },
- 'ID':null,
- 'creationData':{
- '__metadata':{
- 'type':'SP.Social.SocialPostCreationData'
- },
- 'ContentText':'This post was published using REST.',
- 'UpdateStatusText':false
- }
- }
- }),
- headers: {
- "accept": "application/json;odata=verbose",
- "content-type":"application/json;odata=verbose",
- "X-RequestDigest": $("#__REQUESTDIGEST").val()
- },
- success: getMyFeed,
- error: function (xhr, ajaxOptions, thrownError) {
- alert("POST error:\n" + xhr.status + "\n" + thrownError);
- }
- });
- }
- // Get the current user's feed by using the
- // "<app web URL>/_api/social.feed/my/Feed" endpoint.
- function getMyFeed() {
- $.ajax( {
- url: feedManagerEndpoint + "/my/Feed",
- headers: {
- "accept": "application/json;odata=verbose"
- },
- success: feedRetrieved,
- error: function (xhr, ajaxOptions, thrownError) {
- alert("GET error:\n" + xhr.status + "\n" + thrownError);
- }
- });
- }
- // Parse the JSON data and iterate through the feed.
- function feedRetrieved(data) {
- var stringData = JSON.stringify(data);
- var jsonObject = JSON.parse(stringData);
- var feed = jsonObject.d.SocialFeed.Threads;
- var threads = feed.results;
- var newscontent = "";
- for (var i = 0; i < threads.length; i++) {
- var thread = threads[i];
- var participants = thread.Actors;
- var owner = participants.results[thread.OwnerIndex].Name;
- newscontent += '<p>' + owner +
- ' said "' + thread.RootPost.Text + '"</p>';
- }
- $("#message").html(newscontent);
- }
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.

FredPosted Nov 10, 2016, 4:47 AM
At the moment we have this problem (see my last post from yesterday) using the Social Feed REST API in an app context: https://social.msdn.microsoft.com/Forums/office/en-US/5fdab52c-b545-453d-ae3e-053fa3134001/social-feed-rest-api-dont-return-all-threads-in-apphost?forum=appsforsharepoint#b5f5e86a-3dee-432f-be46-1ff462ea59d3 => Newsfeed threads which are related to a site aren't returned, even if the app is deployed in tenant scope and can read all site collections.As you are working in your article with a SharePoint app using the social API, did you ever face this issue or can you confirm or deny it? Thanks!
Vijay SPosted Apr 8, 2015, 9:19 AM
Good one
Gowtham RajamanickamPosted Mar 11, 2015, 10:01 AM
thank u all..
Tom MohanPosted Mar 11, 2015, 4:55 AM
good one.
Rahul Kumar SaxenaPosted Mar 11, 2015, 3:07 AM
Good Show
Prasham SabadraPosted Mar 10, 2015, 10:52 PM
Thanks for Sharing!