Extending the SocialCommentControl

April 6 2011 154 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;
            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&amp;#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: 11% [?]

154 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.
    Regards!

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

Leave a Reply