如何:创建分类多值属性

上次修改时间: 2010年4月14日

适用范围: SharePoint Server 2010

在 Microsoft SharePoint Server 2010 中,您可将用户配置文件属性绑定到一个约束可能值的列表的分类术语集。您可通过创建一个具有多个值的分类属性,将属性与选项列表关联在一起。

有关创建和使用分类术语集的详细信息,请参阅针对 Microsoft SharePoint Server 2010 开发人员的企业元数据管理简介。有关演示如何创建本主题的代码示例中所使用术语集的代码示例,另请参见该主题中的"代码示例:创建并提交分类、添加标签以及删除术语"部分。

以下代码示例演示如何定义具有多个值的分类属性。在运行代码示例之前,请将 servername 替换为实际值。另外,请在您的 Microsoft Visual Studio 项目中添加对以下程序集的引用:

  • Microsoft.Office.Server

  • Microsoft.Office.Server.UserProfiles

  • Microsoft.SharePoint

  • Microsoft.SharePoint.Taxonomy

  • System.Web

示例

using System;
using Microsoft.Office.Server.UserProfiles;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Taxonomy;
 
namespace UserProfilesOMApp
{
    class Program
    {
        static void Main(string[] args)
        {
            // Code example adds a new multi value property named Hobbies.
            using (SPSite site = new SPSite("https://servername"))
            {
                SPServiceContext context = SPServiceContext.GetContext(site);
                UserProfileConfigManager upcm = new UserProfileConfigManager(context);
 
                try
                {
                    ProfilePropertyManager ppm = upcm.ProfilePropertyManager;
 
                    // Create core property.
                    CorePropertyManager cpm = ppm.GetCoreProperties();
                    CoreProperty cp = cpm.Create(false);
                    cp.Name = "Hobbies";
                    cp.DisplayName = "Hobbies";
                    cp.Type = "String (Multi Value)";
                    cp.IsMultivalued = true;
 
                    // Set the TermSet.
                    TaxonomySession taxonomySession = new TaxonomySession(site);
                    TermStore termStore = taxonomySession.TermStores["Managed Metadata Service"];
                    Group group = termStore.Groups["Group1"];
                    TermSet termSet = group.TermSets["TermSet1"];
 
                    cp.TermSet = termSet;
 
                    cpm.Add(cp);
 
                    // Create profile type property.
                    ProfileTypePropertyManager ptpm = ppm.GetProfileTypeProperties(ProfileType.User);
                    ProfileTypeProperty ptp = ptpm.Create(cp);
 
                    ptpm.Add(ptp);
 
                    // Create profile subtype property.
                    ProfileSubtypeManager psm = ProfileSubtypeManager.Get(context);
                    ProfileSubtype ps = psm.GetProfileSubtype(ProfileSubtypeManager.GetDefaultProfileName(ProfileType.User));
                    ProfileSubtypePropertyManager pspm = ps.Properties;
                    ProfileSubtypeProperty psp = pspm.Create(ptp);
 
                    psp.PrivacyPolicy = PrivacyPolicy.OptIn;
                    psp.DefaultPrivacy = Privacy.Organization;
 
                    pspm.Add(psp);
                }
                catch (DuplicateEntryException e)
                {
                    Console.WriteLine(e.Message);
                    Console.Read();
                }
                catch (System.Exception e2)
                {
                    Console.WriteLine(e2.Message);
                    Console.Read();
                }
            }
        }
    }
}

请参阅

任务

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

如何:创建多值属性

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

如何:更改配置文件属性

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