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:
- Create a wiki page
- Add some comments against it
- Delete the wiki page
- Recreate new wiki page with same name
- You can see the comments appearing back
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.
- public static bool DeleteComments(SPSite site, string url)
- {
- bool result = true;
-
- SPServiceContext serviceContext = SPServiceContext.GetContext(site);
- SocialCommentManager manager = new SocialCommentManager(serviceContext);
- SocialComment[] comments = manager.GetComments(new Uri(url), 1000);
- foreach (SocialComment sc in comments)
- {
- try
- {
- PropertyInfo propertyInfo = typeof(SocialComment).GetProperty("CommentID", BindingFlags.NonPublic | BindingFlags.Instance);
- object o = propertyInfo.GetValue(sc, null);
- if (o != null)
- {
- Type t = manager.GetType();
- MethodInfo mi = typeof(SocialCommentManager).GetMethod("DeleteComment",
BindingFlags.NonPublic | BindingFlags.Instance, - null,
- new Type[] { typeof(long) },
- null);
-
- long id = Convert.ToInt64(o.ToString());
- mi.Invoke(manager, new object[] { id });
- }
- }
- catch
- {
- result = false;
- }
- }
- return result;
- }
References
You need to refer the following assemblies:
Running the Code
After running the code I can see the comments are deleted for the given URL.
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.