Extending the SocialCommentControl
SocialCommentControl is the control, which enables commenting on a page. You can use social commenting feature with the Note Board web part (Social Collaboration > Note Board) by adding the web part onto your page. You would have to have User Profile Service Application provisioned to enable social comments. Comments are stored in the Social DB (SocialComments table) of your Service Application.
What we did recently was that we extended the out-of-box SocialCommentControl to be included in a custom page layout with a custom content type. The content type contains a boolean field to indicate, if commenting would be enabled on the published page. Another thing is that we can catch the events when a new comment is added, deleted or updated and build our own custom functionality, if needed, to handle those events.
The first thing was to take a look at the SocialCommentControl class with reflector in Microsoft.SharePoint.Portal assembly. The control inherits from System.Web.UI.WebControls.WebControl and implements the System.Web.UI.WebControls.ICallbackEventHandler. The event handling for comments takes place in RaiseCallBackEvent and the code can be dug out with reflector.
So what if there was a case where the editor wants to specify if commenting is allowed on a published page and wants to get the added comments via email if commenting would be enabled to the page. The scenario is not from our business case and is purely hypothetical but as a demo the case will do just fine.
Below is a quick demonstration of how to handle enabling commenting on a page and get the editor of the page to receive email every time a comment is either added or edited. The sample contains also a workaround for the inconvenient behavior of the out-of-box SocialCommentControl which breaks the DOM and makes web designers mumble when they come across the fact that one div is unclosed.
{
public SocialComments()
{
Load += SocialComments_Load;
}
protected void SocialComments_Load(object sender, EventArgs e)
{
var item = SPContext.Current.ListItem;
// check if the current page has commenting allowed
var allowComments = (item["AllowComments"] != null &&
(bool)(item["AllowComments"])) ? true : false;
if (!allowComments)
{
Visible = false;
return;
}
}
protected override void CreateChildControls()
{
base.CreateChildControls();
// the DOM is a terrible thing to break
// http://www.sharepointblues.com/2010/09/28/fix-disabled-page-scrolling-on-navigation-settings/
Controls.Add(new LiteralControl("</div>"));
}
public string GetCallbackResult()
{
return base.GetCallbackResult();
}
public void RaiseCallbackEvent(string eventArgument)
{
base.RaiseCallbackEvent(eventArgument);
try
{
var item = SPContext.Current.ListItem;
var page = PublishingPage.GetPublishingPage(item);
var editorEmail = page.CreatedBy.Email;
var url = SPContext.Current.Web.Url + "/" + item.Url;
var currentUser = SPContext.Current.Web.CurrentUser.Name;
var document = new XmlDocument();
document.LoadXml(eventArgument);
var documentElement = document.DocumentElement;
/*
eventArgument when added:
<Item type='Add'>
<Title>News</Title>
<RTEContents>This is a new comment</RTEContents>
</Item>
*/
/*
eventArgument when edited
<Item type='Edit'
SequenceId='0'
CommentId='144'
CurrentPage='1' >
<RTEContents>This is an&#160;edited comment</RTEContents>
</Item>
*/
if (documentElement != null)
{
var event = documentElement.GetAttribute("type");
if (event != "Get")
{
if (event == "Add" || event == "Edit")
{
var contentElement =
(XmlElement)documentElement.
SelectSingleNode("./RTEContents");
var content =
SPHttpUtility.NoEncode(contentElement.
InnerText);
var subject =
string.Format("Comment event '{0}' at {1}",
event, DateTime.Now);
var message =
string.Format("Content:
{0}<br/><br/>By {1}<br/><br/>Url: {2}",
content, currentUser, url);
SPUtility.SendEmail(SPContext.Current.Web, true,
false, editorEmail, subject, message);
}
}
}
}
catch(Exception ex)
{
// log the exception
}
}
}
Below is the email the editor would get when a new comment is added to the page where commenting is enabled and the custom extension of the SocialCommentControl is included.

Popularity: 7% [?]
can u please send the source code of this?
Please Send the code to my mail Id as early as possible
Suryanarayana and sd: the code sample is part of the post above. There is nothing more to this. Create a custom content type with boolean field AllowComments, create a page layout bound to your custom content type, add the boolean field to your page layout and add the SocialComments (above) control to your page layout and you are good to go.
Great stuff, thank you for sharing..
Great article. It’s too bad you can’t access the DisplayItems, Uri or AllowNewComments properties in SocialCommentControl in any supported way (no reflection). By using SocialCommentControl in this way you’re more or less stuck with default values (5 comments shown and no way to toggle read-only mode). Any ideas on how to fake read-only mode using SocialCommentControl (i want the comments to be visible, but not the “add comment” field)?
Wow this is great work guys!
I don’t quite understand why Microsoft set up noteboard the way it did. I can set up unique permissions for lists, discussion boards, pages, etc but not a simple comment field?
The bit that really bugs me is not allowing anonymous users to be able to see comments, but not add. Virtually every other website on the planet does this and we want to do this for our external facing website. Any idea how to do this?
Are social comments queried against in searches?
Eric,
as far as i know, the social comments are not being indexed. social tags are, notes arent.
Thanks for this article.
Do you know any way to hide the default message which is posted below the text box when there are no comments yet? This message contains links to the users My Site and Profile which we would prefer not to show…
Thanks, Martin
@Martin — I think your best bet would be to use Javascript to hide the message’s container.