Ads

Friday, 17 February 2017

REST API to get user news feeds in SharePoint 2013

Hope you have idea how to create SharePoint App.
If you are using NAPA tool then replace below codes in APP.js file.
So i am just focusing on the REST script part to do the activities

I recommend to go through REST API for SP document libraries before proceed.

HTML file in Default.aspx 

----------------------
JS script in app.js (Paste the below methods in app.js file) Refer here
-----------------------
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);  
}

No comments:

Post a Comment

Ads