How to retrieve and display social comments in Content Query web part


Using social comments in Publishing pages is pretty common scenario for SharePoint Publishing Sites. In comparison to page fields (Page Content, Title, Page Image and etc.)  that are inserted into the “slots” of the page layout and associated with the content type of the publishing page, social comments are stored separately.  Social comments are stored in separate DB and the comment entry is associated by the page Url on which comment has been given.

Let’s take a look at several approaches how to display social comments for Publishing pages in Content Query web part as shown on picture below.

Prerequisites: to enable social comments in Publishing pages the custom page layout with Note Board web part is used  for demonstration here.


Extending Content By Query web part

In this approach the social comments are retrieved and saved in results before it is sent to the XSL transform using  ProcessDataDelegate delegate of Content Query web part. For retrieving social comments, SocialCommentManager class is utilized, which represents the entry point in  SharePoint object model (OM) that exposes methods to do work with social comments or notes.

So, in order to attach method for processing of social comments to a delegate   ProcessDataDelegate we need to subclass Content Query web part as demonstrated below:

/// <summary>
/// Adding social comments to the results of Content Query web part
/// </summary>
public class SocialCBQ : ContentByQueryWebPart
protected override void OnInit(EventArgs e)
this.ProcessDataDelegate = ProcessSocialData;
private DataTable ProcessSocialData(DataTable data)
var context = SPServiceContext.GetContext(SPContext.Current.Site);
scm = new SocialCommentManager(context);
data.Columns.Add("PageUrl", typeof(string));
data.Columns.Add("PageComments", typeof(int));
foreach (DataRow row in data.Rows)
var fileRefVal = new SPFieldLookupValue((string)row[SPBuiltInFieldId.FileRef.ToString("B")]);
string pageUrl = SPContext.Current.Site.MakeFullUrl("/" + fileRefVal.LookupValue);
var pageComments = scm.GetCount(new Uri(pageUrl));
row["PageUrl"] = pageUrl;
row["PageComments"] = pageComments;
return data;
private SocialCommentManager scm;

view raw


hosted with ❤ by GitHub

And the final step is to customize template for rendering item (ItemStyle.xsl) in order to render social columns  (comment count per page in our case) in Content Query web part:

<div class="comments">
Comments: <span class="comments" pageurl="{@PageUrl}"><xsl:value-of select="@PageComments" /></span>

Utilizing SharePoint Web Services  for retrieving social comment in CQWP

In this approach we are not going to subclass Content Query web part, but implement social comments retrieving and binding on the client side.

SharePoint Web Services exposes Social Data Service that provides methods for remote clients to Create, Read, Update, and Delete (CRUD) social data.

The following JavaScript code demonstrates how to utilize SharePoint Web Services for retrieving comment count per page and to bind it to  comments item template (ItemStyle.xsl)

function CountCommentsOnUrl(url,result)
var soapEnv =
"<soap:Envelope xmlns:xsi='; xmlns:xsd='; xmlns:soap=''&gt; \
<soap:Body> \
<CountCommentsOnUrl xmlns=''&gt; \
<url>" + url + "</url> \
</CountCommentsOnUrl> \
</soap:Body> \
pageurl: url,
result: result,
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(this.result !== undefined) {
var commentCount = $('CountCommentsOnUrlResponse', data).find('CountCommentsOnUrlResult').text();
$(function() {
$('span.comments').each(function() {
var pageUrl = $(this).attr('pageurl');
var comment = $(this);

Code excerpt for comment item from ItemStyle.xsl:

<div class="comments">
Comments: <span class="comments" pageurl="{@LinkUrl}"></span>


5 thoughts on “How to retrieve and display social comments in Content Query web part

  1. Pingback: How to to retrieve and display Social Ratings in Content Query web part | Yet Another SharePoint Blog

    • Add this to your template that you are displaying in your CQWP and it will return all the values from your list:


  2. Hi, how can i do this using content search webpart say i want to display the number of likes and number of comments. Any help appreciated.

Leave a Reply

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

You are commenting using your 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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.