How to: Create Properties with Choice Lists

In Microsoft Office SharePoint Server 2007, you can tie user profile properties to a vocabulary constraining a list of possible values called the choice list. The ChoiceList property on the Property object in the User Profiles object model allows you to associate a choice list with a property. Choice lists can have one of three modes:

  • Closed   Users cannot edit the choices. The administrator is in full control of the choices for the property.

  • Open  Users can add new choices.

  • None  The property is not using a choice list currently, but an administrator can turn on that feature in the future.

The ChoiceList object offers methods to search, remove, and rename vocabulary terms. Once you define a property with a choice list, you cannot redefine it so it does not have the list, nor can you add a list to a property that has not been defined with one.

When you use the object model to try to set a value, but the value is not in the vocabulary list and the list is closed, you get an InvalidValueException. During import, however, Office SharePoint Server 2007 simply ignores the value if the value is not in the list and if the list is closed.

Note

Values are not case-sensitive.

You must have administrative privileges to add properties with choice lists, as well as to set the mode. Administrators can use this feature to control acceptable values for a property. Only administrators can rename and delete choices.

The following code example shows you how to define a property that has a choice list. Replace servername with an actual value before running the code example. Also add references to the following in your Microsoft Visual Studio project:

  • Microsoft.Office.Server

  • Microsoft.SharePoint

  • System.Web

Example

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

Tasks

How to: Create and Edit a User Profile Property
How to: Create Multivalue Properties
How to: Set Multiple Values to a Multivalue Property
How to: Change the Default Separator Character for Entering Multivalue Properties
How to: Set Privacy Policies for User Profile Properties