Export (0) Print
Expand All

How to: Learn to read and write to the social feed by using the REST service in SharePoint 2013

SharePoint 2013

Create a SharePoint-hosted app that uses the REST service to publish a post and get the personal feed for the current user.

This article assumes that you create the app for SharePoint by using "Napa" Office 365 Development Tools on an Office 365 Developer Site. If you're using this development environment, you've already met the prerequisites.

Note Note

Go to Sign up for an Office 365 Developer Site, set up your tools and environment, and start deploying apps to find out how to sign up for a Developer Site and start using "Napa" Office 365 Development Tools.

If you're not using "Napa" Office 365 Development Tools on a Developer Site, you'll need the following:

  • SharePoint Server 2013 with My Site configured, and with a personal site created for the current user

  • Visual Studio 2012 and Office Developer Tools for Visual Studio 2013

  • Full Control access permissions to the User Profile service application for the logged-on user

Note Note

For guidance about how to set up a development environment that fits your needs, see Start building apps for Office and SharePoint.

The SharePoint-hosted app that you create in this article uses JavaScript to build and send HTTP requests to Representational State Transfer (REST) endpoints. These requests publish a post and get the personal feed for the current user. Table 1 contains links to articles that describe general concepts you should understand before you get started.

Table 1. Core concepts for working with SharePoint 2013 social feeds

Article title

Description

Overview of apps for SharePoint

Learn about apps for SharePoint and fundamental concepts for building them.

Get started developing with social features in SharePoint 2013

Find out how to start programming with social feeds and microblog posts, following people and content (documents, sites, and tags), and working with user profiles.

Work with social feeds in SharePoint 2013

Learn about common programming tasks for working with social feeds and the API that you use to perform the tasks.

  1. On your Developer Site, open "Napa" Office 365 Development Tools, and then choose Add New Project.

  2. Choose the App for SharePoint template, name the project SocialFeedREST, and then choose the Create button.

  3. Specify the permissions that your app needs:

    1. Choose the Properties button at the bottom of the page.

    2. In the Properties window, choose Permissions.

    3. In the Content category, set Write permissions for the Tenant scope.

    4. In the Social category, set Read permissions for the User Profiles scope.

    5. Close the Properties window.

  4. Expand the Scripts node, choose the App.js file, and delete the contents of the file.

  1. In the App.js file, declare a global variable for the URL of the SocialFeedManager endpoint.

    var feedManagerEndpoint;
    
  2. Add the following code, which gets the SPAppWebUrl parameter from the query string and uses it to build the SocialFeedManager 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();
    });
    
  3. Add the following code, which builds the HTTP POST request for the /my/Feed/Post endpoint, defines the post's creation data, and publishes the post.

    The request sends a SocialRestPostCreationData resource in the request body. SocialRestPostCreationData contains the target for the post (in this case, null to specify a root post for the current user) and a SocialPostCreationData complex type that defines the post's properties.

    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);
            }
        });
    }
    
    

Add the following code, which gets the Personal feed type for the current user by using the /my/Feed endpoint. The accept header requests that the server return a JavaScript Object Notation (JSON) representation of the feed in its response.

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);
        }
    });    
}

Add the following code, which prepares the returned data by using the JSON.stringify function and the JSON.parse function, and then iterates through the feed and gets the thread's owner and the root post's text.

function feedRetrieved(data) {
    var stringData = JSON.stringify(data);
    var jsonObject = JSON.parse(stringData); 
 
    var feed = jsonObject.d.SocialFeed.Threads; 
    var threads = feed.results;
    var feedContent = "";
    for (var i = 0; i < threads.length; i++) {
        var thread = threads[i];
        var participants = thread.Actors;
        var owner = participants.results[thread.OwnerIndex].Name;
        feedContent += '<p>' + owner + 
            ' said "' + thread.RootPost.Text + '"</p>';
    }  
    $("#message").html(feedContent); 
}

  1. To run the app, choose the Run Project button at the bottom of the page.

  2. In the Do you trust page that opens, choose the Trust It button. The app page opens and displays the owner's name and the text of each root post in the feed.

The following is the complete code example for the App.js file. It publishes a post and gets the personal feed for the current user, which is returned as a JSON object. Then it iterates through the feed.

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 feedContent = "";
    for (var i = 0; i < threads.length; i++) {
        var thread = threads[i];
        var participants = thread.Actors;
        var owner = participants.results[thread.OwnerIndex].Name;
        feedContent += '<p>' + owner + 
            ' said "' + thread.RootPost.Text + '"</p>';
    }  
    $("#message").html(feedContent); 
}
Show:
© 2014 Microsoft