Delete Comments by Note Board Web Part

Introduction

In this article we can explore a problem raised by using Note Board web part and the solution for it.

Note Board Web Part

Note Board web part can be associated with any page. The web part allows comments to be typed by the user.

The Problem

Problem arises when a deleted page is recreated with same name; the old comments also got restored. For generating the problem:

  1. Create a wiki page
  2. Add some comments against it
  3. Delete the wiki page
  4. Recreate new wiki page with same name
  5. You can see the comments appearing back

Wiki Page Content

Solution

The internal class name for Note Board web part is SocialCommentWebPart. SharePoint store the comment information in Social database along with the page URL.

We need to delete the comment from the database. For this we have to use the SocialCommentManager class. Another challenge is there is no public method to delete the comments. We have to use reflection to call the private method.

Code

Following is the code which performs the same. You need to input the URL and a reference site object.

  1. public static bool DeleteComments(SPSite site, string url)  
  2. {  
  3.     bool result = true;  
  4.    
  5.     SPServiceContext serviceContext = SPServiceContext.GetContext(site);  
  6.     SocialCommentManager manager = new SocialCommentManager(serviceContext);  
  7.     SocialComment[] comments = manager.GetComments(new Uri(url), 1000);  
  8.     foreach (SocialComment sc in comments)  
  9.     {  
  10.         try  
  11.         {  
  12.             PropertyInfo propertyInfo = typeof(SocialComment).GetProperty("CommentID",          BindingFlags.NonPublic | BindingFlags.Instance);  
  13.             object o = propertyInfo.GetValue(sc, null);  
  14.             if (o != null)  
  15.             {  
  16.                 Type t = manager.GetType();  
  17.                 MethodInfo mi = typeof(SocialCommentManager).GetMethod("DeleteComment",
    BindingFlags.NonPublic | BindingFlags.Instance,  
  18.                             null,  
  19.                             new Type[] { typeof(long) },  
  20.                             null);  
  21.    
  22.                 long id = Convert.ToInt64(o.ToString());  
  23.                 mi.Invoke(manager, new object[] { id });  
  24.             }  
  25.          }  
  26.          catch  
  27.          {  
  28.                 result = false;  
  29.          }  
  30.     }  
  31.     return result;  

References

You need to refer the following assemblies:

Assemblies in Solution Explorer

Running the Code

After running the code I can see the comments are deleted for the given URL.

Note Board

Note

You can connect the deletion code to a custom event handler for your list/library.

References

http://bit.ly/1rVoo1Z

Summary

In this article we have explored how to delete the comments of a Note Board web part. Please see the console application code attached.