Export (0) Print
Expand All

How to: Create and Retrieve a Social Comment

SharePoint 2010

Last modified: March 15, 2010

Applies to: SharePoint Server 2010

The SocialCommentManager object enables you to create a social comment for any specified URL. This topic demonstrates how to use the SocialCommentManager to create and retrieve social comments in a custom application. The samples in this topic assume that you have added the following references to your Microsoft Visual Studio 2010 project:

  • Microsoft.SharePoint

  • Microsoft.Office.Server

  • Microsoft.Office.Server.UserProfiles

  • System.Web

The overloaded AddComment method creates a single SocialComment object for the current user on a single specified URL. A social comment consists of a URL and a string representing the user’s comment on that URL. A SocialComment also can optionally consist of a string title for the object that is represented by the URL. The default setting for the IsHighPriority Boolean property is false. The sample below demonstrates how to use the SocialCommentManager to add a social comment to a specified URL.

Uri myUri = new Uri("URL");
using (SPSite site = new SPSite("SharePoint site URL"))
{
SPServiceContext context = SPServiceContext.GetContext(site);
SocialCommentManager mySocialCommentManager = new SocialCommentManager(context);
mySocialCommentManager.AddComment(myUri, "comment text");
}

The overloaded GetComments method retrieves all SocialComment objects for a specified URL or user. If you specify a URL, the method returns all social comments added to that URL by the current user in the current SPServerContext. If you specify a user, the method returns all social comments that the specified user added. You can also optionally supply one integer parameter that specifies the maximum number of social comments to return, a second integer parameter that specifies the index number at which to start retrieving social comments from the database, and a DateTime parameter that excludes social comments that are older than the specified time.

using (SPSite site = new SPSite("SharePoint site URL"))
{
SPServiceContext context = SPServiceContext.GetContext(site);
UserProfileManager myUserProfileManager = new UserProfileManager(context);
UserProfile myUserProfile = myUserProfileManager.GetUserProfile(false);
SocialCommentManager mySocialCommentManager = new SocialCommentManager(context);
int maxItems = 10;
SocialComment[] comments = mySocialCommentManager.GetComments(myUserProfile, maxItems);
Console.WriteLine("Comments for user:");
foreach (SocialComment comment in comments)
{
   Console.WriteLine(comment.Url + ": " + comment.Comment);
}
}

Community Additions

ADD
Show:
© 2014 Microsoft