如何:创建包含选项列表的属性

在 Microsoft Office SharePoint Server 2007 中,您可以将用户配置文件属性连接词汇,词汇中限定了称作选项列表 的可能值的列表。通过用户配置文件对象模型中 Property 对象的 ChoiceList 属性,您可以将选项列表与某个属性关联。选项列表可使用以下三种模式之一:

  • Closed   用户无法编辑选项。管理员对属性的选项具有完全控制权限。

  • Open  用户可添加新选项。

  • None  属性当前未使用选项列表,但管理员可在未来启用该功能。

ChoiceList 对象提供了搜索、移除和重命名词汇术语的方法。一旦为某个属性定义了选项列表,便不能重新定义该属性以去除列表,也不能向未定义列表的属性添加列表。

当您尝试使用对象模型来设置值,而该值却不在词汇中并且列表处于关闭状态时,将显示 InvalidValueException。但在导入过程中,如果值不在列表中或列表处于关闭状态,则 Office SharePoint Server 2007 将忽略该值。

备注

值不区分大小写。

您必须具有管理权限才能为属性添加选项列表并设置模式。管理员可使用该功能控制属性的可接受值。只有管理员才可以重新命名选项或删除选项。

以下代码示例演示了如何定义具有选项列表的属性。运行代码示例之前,请将 servername 替换为实际的值。同时在 Microsoft Visual Studio 项目中添加对以下各项的引用:

  • Microsoft.Office.Server

  • Microsoft.SharePoint

  • System.Web

示例

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Office.Server;
using Microsoft.Office.Server.Administration;
using Microsoft.Office.Server.UserProfiles;
using Microsoft.SharePoint;
using System.Web;

namespace UserProfilesApp
{
    class Program
    {
        static void Main(string[] args)
        {

            using (SPSite site = new SPSite("https://servername"))
            {
                ServerContext context =
                    ServerContext.GetContext(site);
                UserProfileManager profileManager = new
                    UserProfileManager(context);
                Property property = profileManager.Properties.Create(false);
                property.Type = PropertyDataType.String;
                property.Name = "Hobbies";
                property.DisplayName = "Hobbies";
                property.Length = 200;
                property.DefaultPrivacy = Privacy.Organization;
                property.PrivacyPolicy = PrivacyPolicy.OptIn;
                property.IsUserEditable = true;
                property.ChoiceType = ChoiceTypes.Closed;
                string[] SampleChoiceList = new string[] { "Xbox", "Bicycling", "Travel" };
                foreach (string choice in SampleChoiceList)
                {
                    property.ChoiceList.Add(choice);
                }
                property.Commit();
            }
        }
    }
}

See Also

任务

如何:创建和编辑用户配置文件属性

如何:创建多值属性

如何:为多值属性设置多个值

如何:更改用于输入多值属性的默认分隔符

如何:为用户配置文件属性设置隐私策略