다음을 통해 공유


How to: Create Multivalue Properties

Applies to: SharePoint Server 2010

Properties now support multiple values. There are many scenarios in which this improvement can be useful; for example, when defining properties that usually contain multiple values, such as a user’s interests and areas of expertise.

The IsMultiValued parameter in the object model indicates whether the property is a multivalue property or not. However, just like the property data type, this parameter is not modifiable, once it is set.

The object model returns the multiple values of a multivalue property as an ArrayList object. The order of the values in the collection is the same as the update order. For an example, see How to: Set Multiple Values to a Multivalue Property.

Multivalue properties are also indexable. Currently, SharePoint Enterprise Search in SharePoint Server 2010 supports Contains and Equals clauses for multivalue properties.

Note that SharePoint Server 2010 allows you to map a multivalue property at the connection source to a single-value portal property. When you import properties, the import operation tries to get the first value from the source.

The following code example shows you how to create multivalue properties. If you use this example, replace servername with an actual value. Also add references to the following in your Microsoft Visual Studio project:

  • Microsoft.Office.Server

  • Microsoft.Office.Server.UserProfiles

  • 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 UserProfilesOMApp
{
    class Program
    {
        static void Main(string[] args)
        {
            //Code example adds a new property called Published Papers.
            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 = "Published Papers";
                    cp.DisplayName = "Published Papers";
                    cp.Type = PropertyDataType.StringMultiValue;
                    cp.Length = 100;
                    cp.IsMultivalued = true;
                    cpm.Add(cp);

                    // create profile type property
                    ProfileTypePropertyManager ptpm = ppm.GetProfileTypeProperties(ProfileType.User);
                    ProfileTypeProperty ptp = ptpm.Create(cp);
                    ptp.IsVisibleOnEditor = true;
                    ptp.IsVisibleOnViewer = true;
                    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.IsUserEditable = true;
                    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();
                }
            }
        }
    }
}

See Also

Tasks

How to: Set Multiple Values to a Multivalue Property

How to: Change Profile Properties

How to: Create Taxonomical Multivalue Properties

How to: Set Privacy Policies for User Profile Properties