代码示例:同事审批社会标记应用程序页

上次修改时间: 2011年8月22日

适用范围: SharePoint Server 2010

"同事审批社会标记应用程序"页是列出同事的应用程序页,您可以在该页上通过选中复选框记录对每个同事的审批。此示例是一个 Microsoft Visual Studio 2010 SharePoint 应用程序页项目。在 Microsoft SharePoint Server 2010 网站上生成并部署此项目后,可以使用此页来表示对同事列表上的任何用户的审批。在当前窗体中,此页只显示一个星号来表示审批。可以将星号替换为适合您的上下文的任何符号或图片。

通过下载 Microsoft SharePoint 2010 软件开发工具包(该链接可能指向英文页面) (SDK) 或通过从代码库(该链接可能指向英文页面)下载示例,将此代码示例安装到您自己的计算机上。如果下载的是 SharePoint 2010 SDK,则此示例将安装在您的文件系统中的以下位置:C:\Program Files\Microsoft SDKs\SharePoint 2010\Samples\Social Data and User Profiles。

使用社会性标签表示项审批

此应用程序通过使用您用直接写入到代码中的网站或网站集创建的 SPServiceContext 对象,创建 UserProfileManager 对象和 SocialTagManager 对象。它还检索表示当前用户的 UserProfile 对象。此应用程序检索当前用户的所有社会性标签,并创建包含该用户同事的名称的字符串列表。表示当前用户同事的每个 UserProfile 对象都具有 PublicUrl 属性。任何可由 URL 表示的对象都可以用社会性标签标记,因此,可将此应用程序修改为允许用户表示对可由 URL 表示的任何项集合的审批,如 SharePoint 列表或文档库中的项。

string mySite = "http://mysite";
protected void Page_Load(object sender, EventArgs e)
{

using ( SPSite site = new SPSite(mySite) )
{


//Create UserProfileManager with desired SPContext.
SPServiceContext serviceContext = SPServiceContext.GetContext(site);
UserProfileManager myUserProfileManager = new UserProfileManager(serviceContext);
UserProfile myUserProfile = myUserProfileManager.GetUserProfile(true);
SocialTagManager mySocialTagManager = new SocialTagManager(serviceContext);
string [] colleagueAccountNames = new string [myUserProfile.Colleagues.GetItems().GetLength(0)];
SocialTag[] mySocialTags = mySocialTagManager.GetTags(myUserProfile);

只有在 User Profile Service 应用程序的权限访问控制列表 (ACL) 中列出的用户才可以创建 UserProfileManager 对象的实例。若要向 User Profile Service 应用程序的权限 ACL 中添加用户,请执行以下过程中的步骤。

向 User Profile Service 应用程序的权限 ACL 中添加用户

  1. 在浏览器中,请转到 SharePoint 管理中心主页。

  2. 导航到"管理服务应用程序"页,该页显示在"应用程序管理"标题下面。

  3. 选择"User Profile Service 应用程序"选项。单击"权限"。

  4. 在此对话框中,添加用户帐户。单击"确定"。

因为社会性标签只使用有效的分类术语,所以此应用程序必须首先执行的任务是从 SocialTagManager 对象中检索 TaxonomySession 对象。然后,此应用程序检查以验证将用于表示审批的分类术语当前是否存在于默认的关键字术语存储区(分类术语的默认位置)中。此示例使用"太棒了"术语,但可更改此术语以满足您的特定需求。还可以将此代码置于 FeatureActivated 事件接收器中,以便只运行一次此代码。

TaxonomySession taxSession = mySocialTagManager.TaxonomySession;
TermStore termStore = taxSession.DefaultKeywordsTermStore;
TermCollection thumbsUp = termStore.KeywordsTermSet.GetTerms("Thumbs Up", true, StringMatchOption.ExactMatch, 1, true);
Term thumbsUpTerm;
if (thumbsUp.Count < 1 || !thumbsUp[0].IsAvailableForTagging)
{
  thumbsUpTerm = termStore.KeywordsTermSet.CreateTerm("Thumbs Up", termStore.DefaultLanguage);
  termStore.CommitAll();
}
else
{
  thumbsUpTerm = thumbsUp[0];
}

然后,此应用程序将每个同事存储在 string 对象数组中。

int n = 0;
foreach (Colleague colleague in myUserProfile.Colleagues.GetItems())
  { 
    colleagueAccountNames[n] = (string)colleague.Profile[PropertyConstants.AccountName].Value;
    n++;
  }

执行完检索用户同事和设置将用于标记每个同事的分类术语的工作之后,此应用程序将检查在某个用户选中或清除显示该用户的每个同事的列表上的复选框后,是否加载了相应页。如果页面加载是用户在窗体中选中或清除复选框的结果,则此应用程序将确定用户选中或清除了哪个同事。对于在名称已清除的任意同事中选中或删除名称的任何同事,向这些同事添加社会性标签。UserProfileManager 对象将检索表示每个同事的 UserProfile 对象,以便它可以在每个同事的公用 URL 中检索、添加或删除社会性标签。此应用程序使用星号 (*) 的 HTML 字符引用来表示当前用户的审批。可以更改或删除此符号来满足您的特定需求。

if (IsPostBack)
{
  for (int i = 0; i < CheckBoxList1.Items.Count; i++)
  {
    UserProfile userProfile = myUserProfileManager.GetUserProfile(CheckBoxList1.Items[i].Value);
    Uri userUrl = userProfile.PublicUrl;
    bool itemProcessed = false;
    foreach (SocialTag tag in mySocialTags)
    {
      if (tag.Term == thumbsUpTerm && tag.Url == userUrl && CheckBoxList1.Items[i].Selected == false)
      {
        mySocialTagManager.DeleteTag(userUrl, thumbsUpTerm);
        CheckBoxList1.Items[i].Text = CheckBoxList1.Items[i].Value;
        itemProcessed = true;
      }
      else if (tag.Term == thumbsUpTerm && tag.Url == userUrl)
      {
        CheckBoxList1.Items[i].Text = CheckBoxList1.Items[i].Value + " &#42;";
        itemProcessed = true;
      }

    }
    if (CheckBoxList1.Items[i].Selected == true && !itemProcessed)
    {
      SocialTag thumbsUpTag = mySocialTagManager.AddTag(userUrl, thumbsUpTerm);
      CheckBoxList1.Items[i].Text = CheckBoxList1.Items[i].Value + " &#42;";
      itemProcessed = true;
    }
    else if (!itemProcessed)
    {
      CheckBoxList1.Items[i].Text = CheckBoxList1.Items[i].Value;
    }
  }
}

如果页面加载是用户在未从列表选中或清除复选框的情况下导航到相应页的结果,则此应用程序将用用户的每个同事的名称填充复选框列表,并检查是否已标记每个同事。

else
{
  CheckBoxList1.DataSource = colleagueAccountNames;
  CheckBoxList1.DataBind();
  for (int i = 0; i < CheckBoxList1.Items.Count; i++)
  {
    UserProfile userProfile = myUserProfileManager.GetUserProfile(CheckBoxList1.Items[i].Value);
    Uri userUrl = userProfile.PublicUrl;
    bool itemProcessed = false;
    foreach (SocialTag tag in mySocialTags)
    {
      if (tag.Term == thumbsUpTerm && tag.Url == userUrl && !itemProcessed)
      {
        CheckBoxList1.Items[i].Selected = true;
        CheckBoxList1.Items[i].Text = CheckBoxList1.Items[i].Value + " &#42;";
        itemProcessed = true;
      }
    }
    if ( !itemProcessed )
    {
      CheckBoxList1.Items[i].Text = CheckBoxList1.Items[i].Value;
    }
  }
}

}
}

生成并运行示例

以下步骤演示如何在开发或测试网站上测试此项目。

生成示例

  1. 创建一个名为 Microsoft.SDK.Server.Samples 的文件夹,然后在该文件夹中解压缩 RatingColleagues.zip 文件。

  2. 启动 Visual Studio 2010,然后打开位于您在步骤 1 创建的文件夹中的 RatingColleagues.sln 文件。

  3. 在"属性"窗口中,指定开发或测试网站的绝对地址的网站 URL 值(例如 http://mysite/)。确保包括末尾的左斜线。另外,将此 URL 作为 ColleagueRating.aspx.cs 文件中的 mySite 变量的值。

  4. 如果这些值尚不存在,则向项目中添加对以下程序集的引用:

    • Microsoft.SharePoint.dll

    • Microsoft.SharePoint.Taxonomy.dll

    • Microsoft.Office.Server.dll

    • Microsoft.Office.Server.UserProfiles.dll

  5. 在"生成"菜单上,选择"部署解决方案"。完成生成后,此应用程序页即会安装在开发或测试网站上。

运行示例

  • 生成并部署解决方案后,请转到 http://mysite/_layouts/ColleagueRating.aspx。您同事的用户帐户名将出现在此页上的相应复选框旁边。

请参阅

概念

通过对象模型创建和使用社交数据

其他资源

代码库(该链接可能指向英文页面)