“读取或更新用户配置文件属性”示例 SharePoint 外接程序

UserProfile.Manipulation.CSOM 示例展示了如何读取并更新特定用户的用户配置文件属性。 此示例使用提供程序托管的外接程序执行以下操作:

  • 读取并显示用户的所有用户配置文件属性。
  • 更新单值用户配置文件属性。
  • 更新多值用户配置文件属性。

如果您需要执行以下操作,请使用此解决方案:

  • 从用户的用户配置文件属性中读取数据或将数据写入用户配置文件属性。
  • 使用用户配置文件属性值个性化设置 SharePoint。

注意

此代码示例仅在 Office 365 上运行。

准备工作

首先,请从 GitHub 上的 Office 365 开发人员模式和做法项目下载 UserProfile.Manipulation.CSOM 示例外接程序。

注意

本文中的代码按原样提供,不提供任何明示或暗示的担保,包括对特定用途适用性、适销性或不侵权的默示担保。

在运行方案 1 之前

  1. 在 Office 365 网站顶部,选择你的配置文件图片,然后选择“关于我”

    突出显示了“关于我”的用户配置文件页面的屏幕截图

  2. “关于我”页上,选择“编辑你的配置文件”

  3. 在“关于我”中,输入“我就职于 Contoso”

  4. 选择“全部保存并关闭”

在运行方案 3 之前

  1. 在网站顶部,选择你的配置文件图片,然后选择“关于我”
  2. “关于我”页上,选择“编辑你的配置文件”
  3. 在“编辑详细信息”中,选择“详细信息”
  4. 在“技能”中,输入“C#、JavaScript”
  5. 选择“全部保存并关闭”

使用 UserProfile.Manipulation.CSOM 示例外接程序

运行本示例时,会启动提供程序托管的外接程序,如下图所示。

UserProfile.Manipulation.CSOM 应用起始页面的屏幕截图

此代码示例包括三个方案。

方案 介绍如何
1 读取运行应用的用户的所有用户配置文件属性。
2* 更新单值用户配置文件属性。
3* 更新多值用户配置文件属性。

注意

此方案仅在 Microsoft 365 中受支持。

方案 1:读取所有用户配置文件属性

选择“运行方案 1”后,外接程序会读取当前用户的所有用户配置文件属性,然后在“当前用户配置文件属性”中显示用户配置文件数据,如下图所示。

当前用户的配置文件属性数据的屏幕截图

选择“运行方案 1”将调用 CodeSample1.aspx.cs 中的 btnScenario1_Click 方法来执行以下任务:

  • 使用 PeopleManager 检索当前用户的所有用户配置文件属性。
  • 遍历 PersonProperties.UserProfileProperties,以在文本框中列出用户配置文件属性值。
protected void btnScenario1_Click(object sender, EventArgs e)
{
  var spContext = SharePointContextProvider.Current.GetSharePointContext(Context);

  using (var clientContext = spContext.CreateUserClientContextForSPHost())
  {
    // Get the people manager instance and load current properties.
    PeopleManager peopleManager = new PeopleManager(clientContext);
    PersonProperties personProperties = peopleManager.GetMyProperties();
    clientContext.Load(personProperties);
    clientContext.ExecuteQuery();

    // Output user profile properties to a text box.
    txtProperties.Text = "";
    foreach (var item in personProperties.UserProfileProperties)
    {
      txtProperties.Text += string.Format("{0} - {1}{2}", item.Key, item.Value, Environment.NewLine);
    }
  }
}

方案 2:更新单值用户配置文件属性

方案 2 显示了如何更新单值用户配置文件属性。 如下图所示,运行此外接程序的用户的“关于我”用户配置文件属性的当前值为“我就职于 Contoso”

方案 2 起始页面的屏幕截图

若要更新“关于我”用户配置文件属性的值,请在“关于我新值”框中,输入“我在 Contoso 担任软件工程师”,然后选择“运行方案 2”。 该代码会将“关于我”属性的值更新为“我在 Contoso 担任软件工程师”

如下图所示,外接程序会将“关于我当前值”更新为“关于我”用户配置文件属性的新值。

已更新“关于我”用户配置文件属性的屏幕截图

选择“运行方案 2”将调用 CodeSample2.aspx.cs 中的 btnScenario2_Click 方法来执行以下任务:

  • 使用 PeopleManager 获取当前用户的用户配置文件属性。
  • 将用户输入的文本格式设置为 HTML。
  • 使用 SetSingleValueProfileProperty 更新 AboutMe 用户配置文件属性的值,SetSingleValueProfileProperty 接受三个参数:
    • 你正在为其更新用户配置文件的用户的帐户名。
    • 用户配置文件属性名(在此方案中,为 AboutMe)。
    • HTML 格式的属性值(在此方案中,为我在 Contoso 担任软件工程师)。
protected void btnScenario2_Click(object sender, EventArgs e)
{
  var spContext = SharePointContextProvider.Current.GetSharePointContext(Context);

  using (var clientContext = spContext.CreateUserClientContextForSPHost())
  {
    // Get the people manager instance and initialize the account name.
    PeopleManager peopleManager = new PeopleManager(clientContext);
    PersonProperties personProperties = peopleManager.GetMyProperties();
    clientContext.Load(personProperties, p => p.AccountName);
    clientContext.ExecuteQuery();

    // Convert entry to HTML.
    string updatedValue = (txtAboutMe.Text).Replace(Environment.NewLine, "");

    // Update the AboutMe property for the user using account name from the user profile.
    peopleManager.SetSingleValueProfileProperty(personProperties.AccountName, "AboutMe", updatedValue);
    clientContext.ExecuteQuery();
  }
}

注意

如果使用自定义用户配置文件属性,请将属性配置为可供用户编辑。 此方案中使用的技术也适用于自定义用户配置文件属性。

方案 3:更新多值用户配置文件属性

方案 3 显示了如何更新多值用户配置文件属性。 下图显示了方案 3 的起始页。 “技能当前值”显示了运行该应用的用户的技能。 从用户的 SPS-Skills 用户配置文件属性中读取技能。

方案 3 起始页面的屏幕截图

从此外接程序向 SPS-Skills 用户配置文件属性添加新技能:

  1. 输入“HTML5”,然后选择“添加技能”。
  2. 输入 ASP.NET,然后选择 “添加技能”。
  3. 选择“运行方案 3”。
  4. 确认“技能当前值”显示用户新的技能列表。
  5. 确认用户的 SPS-Skills 用户配置文件属性现在显示新的技能列表。

选择“运行方案 3”将调用 CodeSample3.aspx.cs 中的 btnScenario3_Click 方法来执行以下任务:

  • 使用 PeopleManager 获取当前用户的用户配置文件属性。
  • 读取列表框中显示的技能列表。
  • 使用 SetMultiValuedProfileProperty 将新技能保存到 SPS-Skills 用户配置文件属性,SetMultiValuedProfileProperty 接受三个参数:
    • 你已为其更新用户配置文件的用户的帐户名。
    • 用户配置文件属性名称,即 SPS-Skills
    • 字符串对象列表形式的属性值。
  protected void btnScenario3_Click(object sender, EventArgs e)
{
  var spContext = SharePointContextProvider.Current.GetSharePointContext(Context);

  using (var clientContext = spContext.CreateUserClientContextForSPHost())
  {
    // Get the people manager instance and initialize the account name.
    PeopleManager peopleManager = new PeopleManager(clientContext);
    PersonProperties personProperties = peopleManager.GetMyProperties();
    clientContext.Load(personProperties, p => p.AccountName);
    clientContext.ExecuteQuery();

    // Collect the user's skills from the list box in order to update the user's profile.
    List<string> skills = new List<string>();
    for (int i = 0; i < lstSkills.Items.Count; i++)
    {
        skills.Add(lstSkills.Items[i].Value);
    }

    // Update the SPS-Skills property for the user using account name from the user's profile.
    peopleManager.SetMultiValuedProfileProperty(personProperties.AccountName, "SPS-Skills", skills);
    clientContext.ExecuteQuery();

    // Refresh the values.
    RefreshUIValues();
  }
}

另请参阅