共用方式為


How to: Create and Retrieve a Social Comment

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

Creating Social Comments

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");
}

Retrieving Social Comments

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);
}
}

See Also

Reference

Microsoft.Office.Server.SocialData

Concepts

How to: Create and Retrieve a Social Rating

How to: Create and Retrieve a Social Tag