SharePoint 2013: Get Top Contributors Amongst the Communities in Social Application
Consolidated Top Contributors.
Now this is interesting, not only administrators but also every community member wants to know who the top contributor on the social application is. And SharePoint understands this requirement and gives the report for every individual community site on the home page.
Description about script
But what if you want the top contributors amongst all communities? As members may have joined more than one community and contributing in all communities; in that case how we can find the top contributors? OOB feature is not there as per my knowledge to fulfill this requirement. So I have created a ECMA script which finds out top 5 contributors from each community and then consolidates them and gives the final result.
So how it works?
- Get all Community sites from the application or site.
- Get top 5 from each community site.
- Then calculate Reputation Score of each member by summing all Reputation Scores of communities he joined.
- Then sorts the data on the basis of Reputation Scores and populate it.
Below screenshot shows the output. This script also sums the number of threads posted and number of replies made my members. Reputation Score is used as criteria to get top contributors.
Script
<head>
<style type="text/css">
.auto-style1 {
width: 169px;
}
</style>
</head>
<script src="/js/jquery-1.7.1.min.js"></script>
<script src="/js/jquery.dataTables.min.js"></script>
<script type="text/javascript">
var allSitesToShow;
var ArrayOfMembers = new Array();
var ArrayOfListItemCollection = new Array();
var context;
$(document).ready(function () {
ExecuteOrDelayUntilScriptLoaded(GetAllCommunitySites, "sp.js");
});
function GetAllCommunitySites() {
debugger;
context = new SP.ClientContext.get_current();
this.Web = context.get_web();
allSitesToShow = this.Web.get_webs();
context.load(this.Web);
context.load(allSitesToShow);
context.executeQueryAsync(Function.createDelegate(this, this.onSuccessAllCommunitySites),
Function.createDelegate(this, this.onFailAllWebs));
}
function onSuccessAllCommunitySites(sender, args) {
debugger;
var camlQuery = new SP.CamlQuery();
//var query = '<View/>';
var query = "<View><Query><OrderBy><FieldRef Name='ReputationScore' Ascending='False'></FieldRef></OrderBy></Query><RowLimit>5</RowLimit></View>";
camlQuery.set_viewXml(query);
context = new SP.ClientContext.get_current();
for (var i = 0 ; i < allwebsToShow.get_count() ; i++) {
if (allwebsToShow.get_item(i).get_webTemplate() == "COMMUNITY") {
//var context = new SP.ClientContext.get_current();
var list = allwebsToShow.get_item(i).get_lists().getByTitle("Community Members");
var listItems_1 = list.getItems(camlQuery);
ArrayOfListItemCollection.push(listItems_1);
context.load(listItems_1);
}
}
context.executeQueryAsync(Function.createDelegate(this, this.onSuccessListItems), Function.createDelegate(this, this.onFailAllWebs));
}
function onSuccessListItems(sender, args) {
debugger;
var flag = false;
for (var i = 0 ; i < ArrayOfListItemCollection.length ; i++) {
for (var j = 0 ; j < ArrayOfListItemCollection[i].get_count() ; j++) {
var Data ={
MemberName: ArrayOfListItemCollection[i].get_item(j).get_fieldValues().Title,
ReputationScore: ArrayOfListItemCollection[i].get_item(j).get_fieldValues().ReputationScore,
NumberOfDiscussions: ArrayOfListItemCollection[i].get_item(j).get_fieldValues().NumberOfDiscussions,
NumberOfReplies: ArrayOfListItemCollection[i].get_item(j).get_fieldValues().NumberOfReplies,
NumberOfBestResponses: ArrayOfListItemCollection[i].get_item(j).get_fieldValues().NumberOfBestResponses,
LookupId: ArrayOfListItemCollection[i].get_item(j).get_fieldValues().Member.get_lookupId()
}
for (var k = 0 ; k < ArrayOfMembers.length ; k++) {
if (ArrayOfMembers[k].MemberName == Data.MemberName) {
flag = true;
break;
}
}
if (flag == true) {
ArrayOfMembers[k].ReputationScore = ArrayOfMembers[k].ReputationScore + Data.ReputationScore;
ArrayOfMembers[k].NumberOfDiscussions = ArrayOfMembers[k].NumberOfDiscussions + Data.NumberOfDiscussions;
ArrayOfMembers[k].NumberOfReplies = ArrayOfMembers[k].NumberOfReplies + Data.NumberOfReplies;
}
else {
ArrayOfMembers.push(Data);
}
flag = false;
}
//alert(ArrayOfMembers);
}
ArrayOfMembers = ArrayOfMembers.sort(function (a, b) { return b.ReputationScore - a.ReputationScore });
var htmlStart = "<table style='width:300px;'>"
var htmlEnd = "</table>"
var htmlstr = "";
for (var i = 0 ; i < ArrayOfMembers.length ; i++) { //ArrayOfMembers.length
if (i == 5)
break;
htmlstr = htmlstr + "<tr style='border-top:1pt solid black;'><td style='width:140px;vAlign:Top;'><a href='/_layouts/15/userdisp.aspx?ID=" + ArrayOfMembers[i].LookupId + "'>" + ArrayOfMembers[i].MemberName + "</a></td><td style='width:140px;'><table><tr><td> Reputation Score: " + ArrayOfMembers[i].ReputationScore + "</td></tr><tr><td> Discussions posted: " + ArrayOfMembers[i].NumberOfDiscussions + "</td></tr><tr><td> Number Of Replies: " + ArrayOfMembers[i].NumberOfReplies + "</td></tr></table></td></tr>";
}
$("#contribDiv").html(htmlStart + htmlstr + htmlEnd);
}
function onFailAllWebs(sender, args) {
alert('Failed:' + args.get_message());
}
</script>
<div>
<div id="contribDiv" style="border:solid 1px #070303;">
</div>
</div>
Conclusion
In this way we can get consolidated data.
See Also
- SharePoint 2013: Social Features, Community Site.
- SharePoint 2013: SharePoint Community Site as Real Time Social Communities or Groups.