如何:创建多值属性
属性现在支持多个值。此改进对许多方案都很有用;例如,当定义通常包含多个值的属性时,如用户的兴趣和的专长。
对象模型中的 IsMultiValued 参数指示属性是否为多值属性。但是,如属性数据类型一样,该参数一经设置便不可修改。
对象模型作为 ArrayList 对象返回多值属性的多个值。集合中值的顺序与更新顺序相同。有关示例,请参阅如何:为多值属性设置多个值。
多值属性也可加索引。目前,Microsoft Office SharePoint Server 2007 企业级搜索 支持多值属性的 Contains 和 Equals 子句。
请注意,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);
try
{
//Get the properties
PropertyCollection pc =
profileManager.Properties;
Property p = pc.Create(false);
p.Name = "PublishedPapers";
p.DisplayName = "Published Papers";
p.Type = "string";
p.IsMultivalued = true;
p.IsUserEditable = true;
p.IsVisibleOnEditor = true;
p.IsVisibleOnViewer = true;
p.DefaultPrivacy = Privacy.Public;
p.PrivacyPolicy = PrivacyPolicy.OptIn;
pc.Add(p); }
catch (DuplicateEntryException e)
{
Console.WriteLine(e.ToString());
}
catch (System.Exception e2)
{
Console.WriteLine(e2.ToString());
}
}
}
}
}