Tag Archives: Newsfeed

Newsfeed Insights

SharePoint Newsfeed Insights

Newsfeed InsightsOne of my clients who is using SharePoint 2013 Sitefeeds extensively in his new social intranet asked me to build ‘something’ to get some insights out of the Social feeds.
So I went and build a little demonstration just to get the following insights out of a SharePoint Site Newsfeed.

  • total number of posts
  • total number of replies
  • total number of likes

I wrote the following peace of JavaScript code to get to the JSON response of a Newsfeed. From there it is just a matter of digging through the structure of the data object.

<html>
  <head>
    <meta charset="utf-8">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js" />
    <script>
      var siteUrl = _spPageContextInfo.webAbsoluteUrl;
      var url = "/_api/social.feed/actor(item=@v)/feed(MaxThreadCount=10000)?@v='" + siteUrl + "/newsfeed.aspx'"

      $.ajax({
        url: url,
        method: "GET",
        headers: { "Accept": "application/json; odata=verbose" },
        dataType: 'json',
        success: function (data) {}, 
        error: function (data) {}
      });
    </script>
  </head>
  <body> 
  </body>
</html>

Here are a couple of examples on what to find were in the JSON Response

Total number of posts
data.d.SocialFeed.Threads.results.length

number of replies of a single post
data.d.SocialFeed.Threads.results[0].TotalReplyCount

number of likes of a single post
data.d.SocialFeed.Threads.results[0].RootPost.LikerInfo.TotalCount

number of likes of a single reply of a post
data.d.SocialFeed.Threads.results[0].Replies.results[0].LikerInfo.TotalCount

Here is the complete JavaScript code of my demo. You can use it in SharePoint WebParts, Add-Ins, Apps, or just put the js file in the Site Assets library and use it in a CEWP (Content Editor Web Part)

<html>
<head>
<meta charset="utf-8">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"/ >
<script>
 var siteUrl = _spPageContextInfo.webAbsoluteUrl;
 var url = "/_api/social.feed/actor(item=@v)/feed(MaxThreadCount=10000)?@v='" + siteUrl + "/newsfeed.aspx'"
 $.ajax({
   url: url,
   method: "GET",
   headers: { "Accept": "application/json; odata=verbose" },
   dataType: 'json',
   success: function (data) {
     if (data.d.SocialFeed.Threads != null) {
       var results = data.d.SocialFeed.Threads.results;
       var numberOfPosts = 0, numberOfReplys = 0, numberOfLikes = 0;
       numberOfPosts = results.length;
       for(var i = 0; i < numberOfPosts; i++) {
         numberOfReplys = numberOfReplys + results[i].TotalReplyCount;
         numberOfLikes = numberOfLikes + results[i].RootPost.LikerInfo.TotalCount;	
         for(var j = 0; j < results[i].Replies.results.length; j++) {
           numberOfLikes = numberOfLikes + results[i].Replies.results[j].LikerInfo.TotalCount;	
         }					
       }

       document.querySelector("#socialThreadStats .sts-posts").innerHTML = numberOfPosts;
       document.querySelector("#socialThreadStats .sts-replys").innerHTML = numberOfReplys;
       document.querySelector("#socialThreadStats .sts-likes").innerHTML = numberOfLikes;
     }
 	else {
 	   document.getElementById("socialThreadStatsStatus").innerHTML = "No Newsfeed found in this web please check configuration"
 	}  
   }, 
   error: function (data) {}
 });
</script>
<style>
  #socialThreadStats {
    display: table;
    padding-top:20px;    
  }
  #socialThreadStats ul {
    display: table-row;
    height: 50px;
    vertical-align: bottom;
  }
  .sts-stat-header {
    display: table-cell;
    width: 120px;
    height: 50px;
    font-size: 16px;
    border-bottom: 2px solid lightgrey;
  }
  .sts-stat-value {
    display: table-cell;
    width: 106px;
    height: 50px;
    font-size: 36px;
    text-align: center;
    border-bottom: 2px solid lightgrey;
  }
</style>
</head>
<body>
  <h1>What´s happening</h1>
  <div id="socialThreadStats">
    <ul>
      <li class="sts-stat-header"><span class="sts-posts-Text">Posts: </span></li>
      <li class="sts-stat-value"><span class="sts-posts"></span></li>
    </ul>
    <ul>
      <li class="sts-stat-header"><span class="sts-replys-Text">Replies: </span></li>
      <li class="sts-stat-value"><span class="sts-replys"></span></li>
    </ul>
    <ul>
      <li class="sts-stat-header"><span class="sts-likes-Text">Likes: </span></li>
      <li class="sts-stat-value"><span class="sts-likes"></span></li>
    </ul>
  </div>
  <div id="socialThreadStatsStatus"></div>
  </body>
</html>

There is also a really good MSDN documentation on social feeds in SharePoint I want to share with you. https://msdn.microsoft.com/en-us/library/office/dn155789.aspx

UPDATE:

I updated the script to be able to get more than the default 20 threads.
You can use the ‘MaxThreadCount’ Property of the feed to get more threads. I set it in the example to feed(MaxThreadCount=10000) to get up to 10000 threads back. A thread is a post in the newsfeed. This does not count for Replies.