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:
-
- var feedManagerEndpoint;
-
-
-
-
- $(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();
- });
-
-
-
-
- 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);
- }
- });
- }
-
-
-
-
- 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);
- }
- });
- }
-
-
-
- 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.