Quantcast
Channel: SharePoint 2013 - Development and Programming forum
Viewing all articles
Browse latest Browse all 7589

Fetching News Feed from other sites

$
0
0

Hi everyone,

Well, I have a problem with fetching the "News Feed" post by JSOM. Due the following code:

<script>
SP.SOD.executeFunc('SP.js', 'SP.ClientContext', function() {
	SP.SOD.executeFunc('userprofile','SP.Social.SocialFeedManager', GetFeeds);
});
var clientContext;
var feedManager;
var ArrayUrl = [];
var web;
var title;
function GetFeeds() {
	for(i=0; i<ArrayUrl.length; i++)
	{
		var p = ReadFeeds(ArrayUrl[i]);
		p.done(function(nf){
			IterateThroughFeed(nf, "News");
		});
		p.fail(function(result){
			console.log(result);
		});
	}
}

function ReadFeeds(url)
{
	var d = $.Deferred();
    // Initialize the current client context and the SocialFeedManager instance.
    clientContext = new SP.ClientContext(url);
    feedManager = new SP.Social.SocialFeedManager(clientContext);
	web = clientContext.get_web();
    // Set parameters for the feed content that you want to retrieve.
    var feedOptions = new SP.Social.SocialFeedOptions();
    feedOptions.set_maxThreadCount(3); // default is 20

    newsFeed = feedManager.getFeedFor(url, feedOptions);

    // Change the sort order to optimize the Timeline feed results.
    feedOptions.set_sortOrder(SP.Social.SocialFeedSortOrder.byCreatedTime);

    clientContext.load(feedManager);
    clientContext.executeQueryAsync(
	function(){
		d.resolve(newsFeed);
	},
	function(){
		d.reject("Error in getting the Newsfeed");
	});

	return d.promise();
}
function IterateThroughFeed(feed, feedType) {
    tblPosts.insertRow().insertCell();
    var feedHeaderRow = tblPosts.insertRow();

    // Iterate through the array of threads in the feed.
    var threads = feed.get_threads();
	console.log(threads + feedType);
    for (var i = 0; i < threads.length ; i++) {
        var thread = threads[i];
        var actors = thread.get_actors();
        if (i == 0) {
            feedHeaderRow.insertCell().innerText = feedType.toUpperCase() + ' FEED FOR '+ title;
        }

        // Use only Normal-type threads and ignore reference-type threads. (SocialThreadType.Normal = 0)
        if (thread.get_threadType() == 0) {

            // Get the root post's author, content, and number of replies.
            var post = thread.get_rootPost();
            var authorName = actors[post.get_authorIndex()].get_name();
            var postContent = post.get_text();
			postContent = buildTextWithUrl(postContent);
            var totalReplies = thread.get_totalReplyCount();
			var date = post.get_createdTime().toString().substring(4,10);

            var postRow = tblPosts.insertRow();
			postRow.insertCell().innerHTML = '<b>' + authorName + ' posted on ' + date + ': </b><br /> \"' + postContent+ '\" (' + totalReplies + ' replies)';

            //postRow.insertCell().innerText = authorName + ' posted \"' + postContent
                //+ '\" (' + totalReplies + ' replies)';

            // If there are any replies, iterate through the array and
            // get the author and content.
            // If a thread contains more than two replies, the server
            // returns a thread digest that contains only the two most
            // recent replies. To get all replies, call the
            // SocialFeedManager.getFullThread method.
            if (totalReplies > 0) {
                var replies = thread.get_replies();

                for (var j = 0; j < replies.length; j++) {
                    var replyRow = tblPosts.insertRow();

                    var reply = replies[j];
                    replyRow.insertCell().innerText = '  - ' + actors[reply.get_authorIndex()].get_name()+ ' replied \"' + reply.get_text() + '\"';
                }
            }
        }
    }

}
   </script><script src='https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js'></script><table width="100%" id="tblPosts"></table><br/>

I use a script editor WP and when "ArrayUrl" has only 1 url (1 site to fetch the newsfeed) works properly. However, when I put more than one site (I want the 3 latest post from different sites) is not working well. Eventhough, sometimes it works and sometimes not... It's not a permission issue because all the sites I have permission.

Can anyone help me to understand what is going on?

Thanks. 



Viewing all articles
Browse latest Browse all 7589

Trending Articles