Getting the Reply Count for a SharePoint Discussion (Discussion List)

SharePoint’s Discussion lists are sort of like Document Sets, in that the original post is a Discussion Content Type which inherits from Folder and the replies are the Message Content Type, which inherits from Item. So there aren’t any Documents involved, but Discussions (Discussion list) are once again Folders.

I was working on task to get the replies of a Discussion in a Discussion list.I implemented the following code using SPServices and was able to get the reply count and replies of a particular discussion.
(ItemCount is that mysterious count of replies I was looking for :))

<!DOCTYPE html>

<html lang=”en” xmlns=””>

<head>
<meta charset=”utf-8″ />
<title>Governance & Nomination Committee</title>

$(document).ready(function() {
var siteURL = _spPageContextInfo.webServerRelativeUrl;
//alert(siteURL);
var discussionListName = “Mergers and Acquisition Committee”;
$().SPServices({
webURL: siteURL,
operation: “GetListItems”,
async: false,
listName: “Mergers and Acquisition Committee”,
CAMLViewFields: “”,
completefunc: function(xData, Status) {
$(xData.responseXML).SPFilterNode(“z:row”).each(function() {
var ItemCount = $(xData.responseXML).SPFilterNode(“rs:data”).attr(“ItemCount”);
//alert(“ItemCount:=” + ItemCount);

var subject, createdOn, modifiedOn, createdOnDate, modifiedOnDate, createdOnTime, modifiedOnTime, discussionURL;
if (ItemCount > 0) {
subject = $(this).attr(“ows_Title”);
createdOn = $(this).attr(“ows_Created”);
modifiedOn = $(this).attr(“ows_Modified”);

createdOnDate = convertDateTime(createdOn, “date”);
//createdOnTime = convertDateTime(createdOn, “time”);

modifiedOnDate = convertDateTime(modifiedOn, “date”);
modifiedOnTime = convertDateTime(modifiedOn, “time”);

var fileRef = $(this).attr(“ows_FileRef”);
//alert(“fileRef:=” + fileRef);
var filepath = getFilePath(fileRef);
//alert(filepath);
var RepliesCount = getDiscussionRepliesCount(discussionListName, filepath, siteURL);
//alert(RepliesCount);

discussionURL = $(this).attr(“ows_EncodedAbsUrl”);
//alert(“discussionURL:=” + discussionURL);
//discussionURL = discussionURL.substr(0, discussionURL.lastIndexOf(“/”));
//alert(“discussionURL:=” + discussionURL);
var disitemURL = discussionURL + “/DispForm.aspx?ID=” + $(this).attr(“ows_ID”);
$(‘#ulMergersNAcquisition’).append(“Title : ” + subject + “” + “Created On : ” + createdOnDate + “” + “Last Updated : ” + modifiedOnDate + “, ” + modifiedOnTime + “” + “Replies : ” + RepliesCount + ““);
} else {
$(‘#ulMergersNAcquisition’).append(“There are no items to show.”);
}

});
} //End of complete function
}); //End of SPServices
}); //End of Doc Ready

function getFilePath(fileRef) {
if (!fileRef) return;
var m = /;#(.*)$/.exec(fileRef);
if (m) {
return m[1];
}
}

function getDiscussionRepliesCount(list, filepath, siteURL) {
//alert(‘In getDiscussionReplies :=’ + filepath);
var i = 0;
var finalReplies;
$().SPServices({
webURL: siteURL,
operation: “GetListItems”,
async: false,
listName: list,
CAMLViewFields: “”,
CAMLQueryOptions: “”,
CAMLQuery: “” + filepath + “”,
completefunc: function(xData, Status) {
$(xData.responseXML).SPFilterNode(“z:row”).each(function() {
//alert(xData.responseText);
if (i == 0) {
var replies = $(this).attr(“ows_ItemChildCount”);
finalRepliesCount = replies.split(‘#’)[1];
//alert(finalRepliesCount);
i = 1;
}
});
//return finalRepliesCount;
} //End of complete function
}); //End of SPServices
return finalRepliesCount;
}

function convertDateTime(x, formatter) {
var months = [‘January’, ‘February’, ‘March’, ‘April’, ‘May’, ‘June’, ‘July’, ‘August’, ‘September’, ‘October’, ‘November’, ‘December’];
var shortMonths = [‘Jan’, ‘Feb’, ‘Mar’, ‘Apr’, ‘May’, ‘Jun’, ‘Jul’, ‘Aug’, ‘Sep’, ‘Oct’, ‘Nov’, ‘Dec’];

// split up date and time
xDate = x.split(” “)[0];
xTime = x.split(” “)[1];

// split off the hour from the minute/second
xMinSec = xTime.split(“:”)[1];
xHour = xTime.split(“:”)[0];

// set the am or pm suffix
if (xHour > 12) {
ampm = “PM”;
xHour -= 12;
} else if (xHour
</head>

<body>
<ul id=”ulMergersNAcquisition”></ul>
</body>
</html>

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s