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

Get Everyone Social Feeds

$
0
0

Hello All,

I tried to retrieve Everyone's social feed using javascript object model but it is only getting feeds from the current logged in user not the whole organization.

please find my code below:-

<table width="100%" id="tblPosts"></table><br /><br /><span id="spanMessage" style="color: #FF0000;"></span><SharePoint:ScriptLink ID="ScriptLink1" Name="SP.js" runat="server" OnDemand="false" Localizable="false" LoadAfterUI="true" /><SharePoint:ScriptLink ID="ScriptLink2" Name="SP.UserProfiles.js" runat="server" OnDemand="false" Localizable="false" LoadAfterUI="true" /><SharePoint:FormDigest ID="FormDigest" runat="server" /><script type="text/javascript">

    /*
 * JavaScript Pretty Date
 * Copyright (c) 2011 John Resig (ejohn.org)
 * Licensed under the MIT and GPL licenses.
 */

    // Takes an ISO time and returns a string representing how
    // long ago the date represents.
    function prettyDate(time) {
        //2008-01-26T22:24:17Z
        var timeString = time.getFullYear() + "-" + (time.getMonth() + 1) + "-" + time.getDate() + "T" + time.getHours() + ":" + time.getMinutes() + ":" + time.getSeconds() + "Z";
        var date = new Date((timeString || "").replace(/-/g, "/").replace(/[TZ]/g, " ")),
            diff = (((new Date()).getTime() - date.getTime()) / 1000),
            day_diff = Math.round(diff / 86400);

        if (isNaN(day_diff) || day_diff < 0 || day_diff >= 31)
            return;
        if (day_diff > 6) {
            var format = "dd MMM, yyyy";
            return time.format(format);
        }
        return day_diff == 0 && (
                diff < 60 && "just now" ||
                diff < 120 && "1 minute ago" ||
                diff < 3600 && Math.round(diff / 60) + " minutes ago" ||
                diff < 7200 && "1 hour ago" ||
                diff < 86400 && Math.round(diff / 3600) + " hours ago") ||
            day_diff == 1 && "Yesterday" ||
            day_diff < 7 && day_diff + " days ago";
    }

    // If jQuery is included in the page, adds a jQuery plugin to handle it as well
    if (typeof jQuery != "undefined")
        jQuery.fn.prettyDate = function () {
            return this.each(function () {
                var date = prettyDate(this.title);
                if (date)
                    jQuery(this).text(date);
            });
        };

    // Replace the placeholder value with the account name of the target user.

    // Ensure that the SP.UserProfiles.js file is loaded before the custom code runs.
    SP.SOD.executeOrDelayUntilScriptLoaded(StartFunction, 'SP.UserProfiles.js');

    // Declare global variables.
    var clientContext;
    var feedManager;
    var everyoneFeed;
    function StartFunction() {
        GetFeeds();
        setInterval(GetFeeds, 30000);
    }
    function GetFeeds() {
        
        // Initialize the current client context and the SocialFeedManager instance.
        clientContext = SP.ClientContext.get_current();
        feedManager = new SP.Social.SocialFeedManager(clientContext);

        // Set parameters for the feed content that you want to retrieve.
        var feedOptions = new SP.Social.SocialFeedOptions();
        feedOptions.set_maxThreadCount(5);

        // Get all feed types for current user and get the Personal feed
        // for the target user.
        everyoneFeed = feedManager.getFeed(SP.Social.SocialFeedType.everyoneFeed, feedOptions);
        // Change the sort order to optimize the Timeline feed results.
        feedOptions.set_sortOrder(SP.Social.SocialFeedSortOrder.byCreatedTime);

        clientContext.load(feedManager);
        clientContext.executeQueryAsync(CallIterateFunctionForFeeds, RequestFailed);

    }
    function CallIterateFunctionForFeeds() {
        IterateThroughFeed(everyoneFeed, "Everyone", true);
    }
    function IterateThroughFeed(feed, feedType, isCurrentUser) {
        var tblPosts = document.getElementById('tblPosts');
        tblPosts.insertRow().insertCell();
        var feedHeaderRow = tblPosts.insertRow();

        // Iterate through the array of threads in the feed.
        var threads = feed.get_threads();
        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();
            }

            // 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();
                var totalReplies = thread.get_totalReplyCount();
                var postDate = prettyDate(post.get_createdTime());

                var author = actors[post.get_authorIndex()];
                var authorLoginName = author.get_accountName();
                var authorImage = author.get_imageUri();
                var authorSite = author.get_uri();

                var postRow = tblPosts.insertRow();
                postRow.insertCell().innerHTML = "<img src='" + authorImage + "'/>";
                postRow.insertCell().innerHTML = "<a href='" + authorSite + "'>" + authorName + "</a>";
                postRow.insertCell().innerText = postDate;
                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() + '\"';
                    }
                }
            }
        }
    }
    function RequestFailed(sender, args) {
        $get("spanMessage").innerText = 'Request failed: ' + args.get_message();
    }</script>



Viewing all articles
Browse latest Browse all 7589

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>