Extending the SocialCommentControl

April 6 2011 164 comments

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 class SocialComments : SocialCommentControl, ICallbackEventHandler
    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;

    protected override void 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)

            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();
            var documentElement = document.DocumentElement;

            eventArgument when added:
            <Item  type='Add'>
                <RTEContents>This is a new comment</RTEContents>

            eventArgument when edited
            <Item type='Edit'
            CurrentPage='1' >
            <RTEContents>This is an&amp;#160;edited comment</RTEContents>

            if (documentElement != null)
                var event = documentElement.GetAttribute("type");
                if (event != "Get")
                    if (event == "Add" || event == "Edit")
                        var contentElement =
                        var content =
                        var subject =
                        string.Format("Comment event '{0}' at {1}",
                        event, DateTime.Now);
                        var message =
                        {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: 11% [?]

164 comments to “Extending the SocialCommentControl”

  1. Mckenzie says:

    Please let me know if you’re looking for a article author for your site.

    You have some really great posts and I think I would be a good asset.
    If you ever want to take some of the load off, I’d love to write some content
    for your blog in exchange for a link back to mine. Please shoot me an e-mail if interested.

  2. Link exchange is nothing else however it is simply placing the other person’s webpage link on your page at appropriate place
    and other person will also do similar for you.

  3. Hey there, I think your website might be having browser compatibility issues.
    When I look at your website in Safari, it looks fine but when opening in Internet
    Explorer, it has some overlapping. I just wanted to
    give you a quick heads up! Other then that, excellent blog!

  4. お肌が弱くて化粧水をつけるとお肌が赤くなってしまう方。敏感肌でお化粧もできない。敏感用のものを使ってもヒリヒリして痒くなってしまって、自分に合うものがどうしても見つからない人はオージュンヌという化粧水を試してみるといいかもしれません。もちろん香料などの不要なものは一切入っていないですから安心して使えます。アトピーでお化粧ができなかったという人でも、お肌の状態が良くなったケースがあります。ヒリヒリして赤みが出てしまったり、保湿タイプを使うと逆にニキビができてしまったりという方に向いています。

  5. webpage says:

    I notice there is certainly lots of spam on this blog. Do you need help cleaning them up? I might help between courses!

  6. Grace says:

    I die ganze Zeit mailte diese weblog Post-Seite, um allle meine Freunde Mitarbeiter, aaus
    dem Grund,, wenn es gerne lesen danach meiner Freunde auch.

  7. N級品 says:

    ロレックス、ブルガリ、フランク ミュラー、
    オーデマ ピゲ、ブライトリング、グッチ、エルメス、パネライ、

  8. erotic says:

    Yoou actually make it seem so easy along with your presentation however I in finding this topic to be realoy something tjat I think I’d by no means understand.
    It seems too compllicated and very huge for me. I’m taking a look forward in your next put up, I will try too get
    the hold of it!

    Alsso visit my blog … erotic

  9. If you do what you always did, you will get what you always got. ~Anonymous

  10. zobacz tutaj says:

    Aw, this was an incredibly good post. Taking the time and actual effort to produce a great article… but what can I say… I
    procrastinate a lot and don’t seem to get nearly
    anything done.

  11. Aw, thiѕ was ɑ really ցood post. Finding the time and actual effort to generate a νery gooԀ article… bᥙt whɑt саn I
    ѕay… I hesitate а lot and never seem tⲟ get anythіng done.

  12. zobacz tutaj says:

    Do you mind if I quote a few of your posts as long as I provide credit and sources back to your website?

    My blog is in the exact same niche as yours and my users would genuinely
    benefit from a lot of the information you provide here. Please let me know
    if this alright with you. Many thanks!

  13. Hi there, everything is going well here and ofcourse every one is sharing data,
    that’s in fact fine, keep up writing.

  14. It’s genuinely very difficult in this busy life to listen news on Television, so I only use internet for that purpose,
    and obtain the latest information.

Leave a Reply