您的位置:首页 > 编程语言

SharePoint 2013 中使用代码取特定用户的social tag

2013-11-07 19:34 387 查看
本文讲述如何使用SharePoint 2013 中使用代码取特定用户的social tag。

1. 首先确保Social feature 配置正确,可以打tag,笔者打了三个tag



2. 新建一个Console application,将其target platform 改成X64

3. 引用如下Dll 

Microsoft.Office.Server.UserProfiles.dll

 Microsoft.SharePoint.dll

Microsoft.SharePoint.Taxonomy.dll 

 Microsoft.Office.Server.dll 

3. Program.cs代码 :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint;
using Microsoft.Office.Server.SocialData;
using Microsoft.SharePoint.Utilities;
using Microsoft.Office.Server.UserProfiles;

namespace GetTags
{
class Program
{
static void Main(string[] args)
{
if (args.Length == 0)
{
Console.WriteLine("Please use: GetTags SiteUrl");
}
else
{
using (SPSite site = new SPSite(args[0]))
{
using (SPWeb web = site.OpenWeb())
{
// 取 SocialTagManager
SPServiceContext serviceContext = SPServiceContext.GetContext(site);
SocialTagManager stm = new SocialTagManager(serviceContext);

Console.WriteLine("Get profile manager");
UserProfileManager profileManager = new UserProfileManager(serviceContext);

// 如果在web part中 userloginName可以传 SPContext.Current.Web.CurrentUser.LoginName
// 在Console application中使用用户的alias,不带域名
Console.WriteLine("Get user profile");
UserProfile profile = profileManager.GetUserProfile("userloginName");
Uri uri = new Uri(web.Url);
Console.WriteLine("Start request");

// 取对应的用户的social tags
var itemTags = stm.GetTags(profile);

// 打印  soical tags
Console.WriteLine("----------------------------------------------");
Console.WriteLine("Tags count: " + itemTags.Length);
foreach (var tag in itemTags)
{
Console.WriteLine(string.Format("Title: {0} - URL: {1}, Term: {2}",
tag.Title,
tag.Url,
tag.Term.GetDefaultLabel(1033)));
}

// 可以使用 itemTags[0].Delete(); 删除tag
// 可以使用 itemTags[0].Update(); 更新tag
}
}
}

Console.WriteLine("Press any key to exit");
Console.ReadKey();
}
}
}


4. 结果为:



5. 可以在 .../my/_layouts/15/thoughts.aspx 中删除tag

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐