Get Comment Count in SharePoint


Hi Guys,

In this blog I will explain how we can get the number of comments on any page/article. Prerequisites for this blog are: a list with rating enabled, an article/page/splistItem already have got comments from users and a tool like VS to code 😉

According to requirement it may be the case that one need to get item comment count using java script or c#. I will explain both way to get the comment count:

Using Javascript: This may be the case when one want to use the content search webpart or CQWP to display comment count. Followng code can be use to get the comment Count:

var linkURL = $getItemValue(ctx, “Link URL”);
linkURL.overrideValueRenderer($urlHtmlEncode);

// Get the Comments count
var soapEnv =
“<soap:Envelope xmlns:xsi=’http://www.w3.org/2001/XMLSchema-instance&#8217; xmlns:xsd=’http://www.w3.org/2001/XMLSchema&#8217; xmlns:soap=’http://schemas.xmlsoap.org/soap/envelope/’&gt; \
<soap:Body>    \
<CountCommentsOnUrl xmlns=’http://microsoft.com/webservices/SharePointPortalServer/SocialDataService’&gt; \
<url>” + linkURL + “</url> \
</CountCommentsOnUrl> \
</soap:Body> \
</soap:Envelope>”;

$.ajax({
pageurl: linkURL,
url: “/_vti_bin/SocialDataService.asmx?op=CountCommentsOnUrl”,
type: “POST”,
dataType: “xml”,
data: soapEnv,
contentType: “text/xml; charset=\”utf-8\””,
success: function(data, status, xhr){
if(data !== undefined) {
var commentCount = $(‘CountCommentsOnUrlResponse’, data).find(‘CountCommentsOnUrlResult’).text();
if(commentCount>0){
$(‘#’+ commentId +  ‘ .comments’).html(commentCount);
$(‘#’+ commentId).show();
}
else{
$(‘#’+ commentId).hide();
}
}
}
});

Using server code: You must have to know the full path of the page. That is again a challenge. In most of the cases you must be having SPListItem object. Following code can be used to get the path:

var pageUrl = item[“EncodedAbsUrl”] + item[“FileRef”].ToString().Substring(item[“FileRef”].ToString().IndexOf(“#”) + 1);

var context =SPServiceContext.Current;
var scManager =newSocialCommentManager(context);
var pageUri =newUri(pageUrl);
int count = scManager.GetCount(pageUri).ToString(CultureInfo.InvariantCulture);

Hope above code helped you. Kindly comment and share your views.

Happy SharePointing 🙂

 

Advertisements

2 thoughts on “Get Comment Count in SharePoint

  1. It was difficult to find your site in google.
    You should create some high PR contextual backlinks in order to rank your
    blog. I know – writing articles is very time consuming,
    but contextual backlinks are the best type of backlinks.
    I know very useful tool that will help you to create unique, readable content in minute, just type in google – masagaltas
    free content

    Like

  2. VMware Certified Advanced Professional 6 (Desktop and Mobility Deployment) – The industry-recognized VCAP6-DTM Deploy certification validates that you know how to deploy and optimize VMware Horizon 6 (with View) environments. It proves that you have the knowledge and expertise vital to leverage best practices to provide a scalable and dependable Business Mobility platform for your company. Some of the topics involve: Configuring and managing Horizon View components, configuring cloud pod archituecture, configuring Group Policy settings related to Horizon View, Configuring and optimizing desktop images for Horizon View & Mirage, Configuring and managing App Volumes AppStacks, Configuring desktop pools, Configuring and deploying ThinApp packaged applications, Configuring VMWare Identity Manager, etc.Sebastian’s take on the VCAP6 examination: “In my point of view VCAP6 exam is way better experience when compared with VCAP5, the new examination appears to be just like VMware HOL. The screen is easy, questions are prepared on the right area of the display, and could be concealed to the side or restored when necessary. My bits of advice to the questions windowpane: if you would like make it floating, you must know how to restore it back. I ended up shifting it all around for the reason that I forget about how to recover it back. The two arrows that appeared to be control buttons on top were used to dock the window to left or right. Fonts can be resized, which from my opinion was a lot better than scrolling up and down the question. The reaction speed of the whole interface was so considerably quicker than VCAP5.5, and there wasn’t any lagging period experienced when switching from window to window. One thing to bear in mind: BACKSPACE key is not working! I think this is good because you don’t reload your examination window by mistake, yet, it could be troublesome in some cases when you type some thing mistakenly and you need to select and press Del to remove. The Desktop and shortcuts were sorted really well, and all necessary applications like browser or Mirage console are easily launched. You will find there’s pleasant user interface for Remote Desktop Manager where you can find all essential RDP connection to servers or desktops with no need to type account information. The web browser had all the links in the Favorite Bar. At the time I’m writing this, there’s no additional Thirty minute extension for Non-Native English speaker at No-Native English country, which is actually a bummer. There are 39 question to answer within the three hours period, and this can be actually quite hard for non-native English speakers like me. Quite a few questions take time to finish, so it is far better to omit the questions that you cannot answer, and complete those you can. After the thirty-nine questions, you’ll be able to revisit the uncompleted questions if you have time. Never waste a lot of time on one single question! The exam blue print is available on my website at Szumigalski.com. It is well-organized and following it for the exam preparation will be helpful to a lot. Obviously, the most effective is if you could have numerous practical experience! I’m actually very pleased with the examination experience, although I passed this time around by small margin, but I understand what I missed for the examination, learn from the errors and practice harder to acquaint myself with the environment. This certification will definitely open up your job prospects!”

    Like

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