如何:创建和检索社交标记
上次修改时间: 2010年3月30日
适用范围: SharePoint Server 2010
SocialTagManager 对象允许您通过所用分类中的有效术语为任何指定的 URL 创建社会性标签。本主题演示如何使用 SocialTagManager 在自定义应用程序中创建和检索社会性标签。有关演示如何执行本主题中所含任务的代码示例,请参阅代码示例:社会数据统计信息 Web 部件和代码示例:同事审批社会标记应用程序页。本主题中的示例假定您已经向 Microsoft Visual Studio 2010 项目添加了以下引用:
Microsoft.SharePoint
Microsoft.Office.Server
Microsoft.Office.Server.UserProfiles
System.Web
创建社会性标签
AddTag 重载方法在单个指定 URL 上为当前用户创建单个 SocialTag 对象。社会性标签由 URL 和所用分类中的术语组成。如果分类 Term 不存在,则必须将其添加到术语库中。SocialTag 还可以包含对象(由标记的 URL 表示)的字符串标题以及一个布尔值,该布尔值确定社会性标签是否为私有标签并且仅对当前用户可见。IsPrivate 布尔属性的默认设置为 false,因此社会性标签默认为公共标签。下面的示例演示如何向默认关键字术语库中添加术语,然后使用 SocialTagManager 将该术语作为单个标签添加到指定的 URL。
Uri myUri = new Uri("URL");
using (SPSite site = new SPSite("SharePoint site URL"))
{
SPServiceContext context = SPServiceContext.GetContext(site);
SocialTagManager mySocialTagManager = new SocialTagManager(context);
//Retrieve the taxonomy session from the SocialTagManager.
TaxonomySession taxSession = mySocialTagManager.TaxonomySession;
TermStore termStore = taxSession.DefaultKeywordsTermStore;
myTerm = termStore.KeywordsTermSet.CreateTerm("term", termStore.DefaultLanguage);
termStore.CommitAll();
mySocialTagManager.AddTag(myUri, myTerm);
}
检索社会性标签
GetTags 重载方法检索指定 URL 或用户的所有 SocialTag 对象。如果指定 URL,则该方法返回由当前用户在当前 SPServerContext 中向该 URL 添加的所有社会性标签。如果指定用户,则该方法返回指定的用户添加的所有社会性标签。
using (SPSite site = new SPSite("SharePoint site URL"))
{
SPServiceContext context = SPServiceContext.GetContext(site);
UserProfileManager myUserProfileManager = new UserProfileManager(context);
UserProfile myUserProfile = myUserProfileManager.GetUserProfile(false);
SocialTagManager mySocialTagManager = new SocialTagManager(context);
SocialTag[] tags = mySocialTagManager.GetTags(myUserProfile);
Console.WriteLine("Tags for user:");
foreach (SocialTag tag in tags)
{
Console.WriteLine(tag.Term.Name + ": " + tag.Uri.AbsoluteUri);
}
}
请参阅
引用
Microsoft.Office.Server.SocialData