Export (0) Print
Expand All

How to: Create Taxonomical Multivalue Properties

Last modified: April 14, 2010

Applies to: SharePoint Server 2010

In Microsoft SharePoint Server 2010, you can tie user profile properties to a taxonomy term set constraining a list of possible values. You can associate a property with a list of choices by creating a taxonomic property with multiple values.

See A Brief Introduction to Enterprise Metadata Management for Microsoft SharePoint Server 2010 Developers for more information about creating and using taxonomies and term sets. Also see the Code Sample: Create and Commit a Taxonomy, Add Labels, and Delete a Term section in that topic for a code sample that demonstrates how to create the term set that is used in the code sample in this topic.

The following code example shows how to define a taxonomical property with multiple values. Replace servername with an actual value before running the code example. Also add references to the following assemblies in your Microsoft Visual Studio project:

  • 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("http://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();
                }
            }
        }
    }
}

Community Additions

ADD
Show:
© 2014 Microsoft