上次修改时间: 2011年8月22日
适用范围: SharePoint Server 2010
社交数据统计 Web 部件可显示社交数据统计信息。此示例是一个 Microsoft Visual Studio 2010 SharePoint Visual Web 部件项目。在 Microsoft SharePoint Server 2010 网站上生成并部署该项目后,可将此 Web 部件添加到要显示用户的社会性标签活动统计信息的任何页面。此 Web 部件在三个表中显示了以下信息:
每个已标记的 URL 以及用于标记每个 URL 的字词
社会性标签中已使用的每个字词以及该字词已使用的次数
已添加社会性标签的每个用户以及用户已标记 URL 的次数
通过下载 Microsoft SharePoint 2010 软件开发工具包(该链接可能指向英文页面) (SDK) 或通过从代码库(该链接可能指向英文页面)中下载示例,将此代码示例安装到您自己的计算机上。如果下载的是 SharePoint 2010 SDK,则此示例将安装到您的文件系统中的以下位置:C:\Program Files\Microsoft SDKs\SharePoint 2010\Samples\Social Data and User Profiles。
使用对象模型检索和存储数据
社交数据统计 Web 部件通过使用您用直接写入代码的网站或网站集创建的 SPServiceContext 对象,来创建 UserProfileManager 对象和 SocialTagManager 对象。
//Change this value to the root URL for your site.
string socialDataStatsSite = "http://mysite";
using (SPSite siteColl = new SPSite(socialDataStatsSite))
{
//Create SocialTagManager with desired SPServiceContext.
SPServiceContext serviceContext = SPServiceContext.GetContext(siteColl);
UserProfileManager myUserProfileManager = new UserProfileManager(serviceContext);
SocialTagManager mySocialTagManager = new SocialTagManager(serviceContext);
仅 User Profile Service 应用程序的"权限"访问控制列表 (ACL) 中列出的用户可以创建 UserProfileManager 对象实例。若要向 User Profile Service 应用程序的权限 ACL 中添加用户,请执行以下过程中的步骤。
向 User Profile Service 应用程序的权限 ACL 中添加用户
在浏览器中,转到 SharePoint 管理中心主页。
导航到"管理服务应用程序"页,该页显示在"应用程序管理"标题下方。
选择"User Profile Service 应用程序"选项。单击"权限"。
在该对话框中,添加用户帐户。单击"确定"。
由于社会性标签只使用有效的分类字词,因此该 Web 部件必须执行的第一个任务是,在当前分类存储区中检索已创建的所有社会性字词。每个 SocialTerm 对象均包含来自当前分类存储区的分类字词和一个表示该字词在社会性标签中已使用的次数的 Count 属性。
//Get all social terms and store in an array.
SocialTerm[] socTerms = mySocialTagManager.GetAllTerms();
下一步是创建可存储此 Web 部件显示的所有信息的 Dictionary 对象。
//Create a Dictionary to store values for tags on social URLs, number of times each social term is used,
//and number of times that each user has tagged a URL.
Dictionary<string, string> SocialUrlStats = new Dictionary<string, string>();
Dictionary<string, long> SocialTermStats = new Dictionary<string, long>();
Dictionary<string, int> SocialTagStats = new Dictionary<string, int>();
在此 Web 部件检索到社会性标签中已使用的所有社会性字词后,它会循环访问社会性字词的数组,再存储每个字词的计数,然后检索并存储已使用每个字词标记的所有 URL。
//Get a Taxonomy term for each SocialTerm, get the number of times each social term has been used in a tag,
//and get all the URLs for which that term has been used as a tag. Then map each URL to each of the terms with which
//it has been tagged.
foreach (SocialTerm aTerm in socTerms)
{
string termName = aTerm.Term.Name;
SocialTermStats.Add(termName, aTerm.Count);
try
{
SocialUrl[] socURLs = mySocialTagManager.GetAllUrls(aTerm.Term);
foreach (SocialUrl aUrl in socURLs)
{
string urlString = aUrl.Url.ToString();
if (!SocialUrlStats.ContainsKey(urlString))
{
SocialUrlStats.Add(urlString, termName);
}
else
{
SocialUrlStats[urlString] += ", " + termName;
}
}
}
catch (UserNotFoundException unfE)
{
// If the user is not found, handle exception.
}
}
然后,此 Web 部件通过检索每个用户在 UserProfileManager 中创建的所有社会性标签来填充 SocialTagStatsDictionary 对象。之后,它将每个社会性标签映射到其所有者。
//Get all the social tags in which the terms have been used.
List<SocialTag> socialTagsList = new List<SocialTag>();
foreach (UserProfile userProf in myUserProfileManager)
{
try
{
SocialTag[] userTags = mySocialTagManager.GetTags(userProf, 0);
foreach (SocialTag aTag in userTags)
{
socialTagsList.Add(aTag);
}
}
catch (UserNotFoundException unfE )
{
//If the user is not found, handle exception.
}
}
//For each SocialTag, get the owner and get the number of times that owner has tagged a URL.
foreach (SocialTag aTag in socialTagsList)
{
if (aTag != null)
{
if (!SocialTagStats.ContainsKey(aTag.Owner.DisplayName))
{
int tagCount = mySocialTagManager.GetCount(aTag.Owner);
SocialTagStats.Add(aTag.Owner.DisplayName, tagCount);
}
}
}
最后,此 Web 部件会将每个 Dictionary 对象绑定到一个 GridView 对象,以显示它检索到的社交数据统计信息。用户界面中使用的字符串将存储在一个资源文件中。
//Bind each Dictionary to a GridView control. Display localized header strings for each column.
GridView1.DataSource = SocialUrlStats;
GridView1.Columns[0].HeaderText = Properties.Resources.String1;
GridView1.Columns[1].HeaderText = Properties.Resources.String2;
GridView1.DataBind();
GridView2.DataSource = SocialTermStats;
GridView2.Columns[0].HeaderText = Properties.Resources.String3;
GridView2.Columns[1].HeaderText = Properties.Resources.String4;
GridView2.DataBind();
GridView3.DataSource = SocialTagStats;
GridView3.Columns[0].HeaderText = Properties.Resources.String5;
GridView3.Columns[1].HeaderText = Properties.Resources.String2;
GridView3.DataBind();
}
生成并运行示例
下列步骤演示如何在开发或测试网站上测试此项目。
生成示例
创建一个名为 Microsoft.SDK.Server.Samples 的文件夹,然后在该文件夹中解压缩 SocialDataStatistics.zip 文件。
启动 Visual Studio 2010,然后在步骤 1 中创建的文件夹中打开 SocialDataStatistics.sln 文件。
在"属性"窗口中,指定开发或测试网站的绝对地址的网站 URL 值(例如 http://mysite/)。确保包含结束左斜线。此外,将此 URL 作为 VisualWebPart1UserControl.ascx.cs 文件中的 socialDataStatsSite 字符串的值。
如果它们尚不存在,则向项目添加对以下程序集的引用:
Microsoft.SharePoint.dll
Microsoft.SharePoint.Taxonomy.dll
Microsoft.Office.Server.dll
Microsoft.Office.Server.UserProfiles.dll
在"生成"菜单上,选择"部署解决方案"。完成此生成后,在开发或测试网站上安装此 Web 部件。
运行示例
向网站上的任何页面添加新安装的 Web 部件。从"自定义"类别选择"VisualWebPartProject1"Web 部件。
提示
如果页面生成一条错误,指示无法找到此 Web 部件的控件,则导航到 \Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\CONTROLTEMPLATES,然后将 SocialDataStatisticsWebPart 重命名为 VisualWebPartProject1。