Share via


How to: Create and Edit a User Profile Property

Applies to: SharePoint Server 2010

User profile properties are name-value pairs attached to user profiles that describe personal information about the user. The profile store contains a list of user profile property information. This information is obtained by importing it from a directory that contains user accounts or manually by typing the account information into the user profile store. By default, Microsoft SharePoint Server 2010 can import from the Active Directory directory service, LDAP servers, and Business Data Catalog.

SharePoint Server 2010 provides a default set of commonly used user profile properties. Sometimes these may not be sufficient, and you might need additional properties. In such cases, you can create new properties, and they will be available for all user profiles. The following code example shows you how to add a new user profile property to the default set of properties and how to set privacy policies for the new property.

Replace servername with an actual value before using the code example. 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 Marital Status.
            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 = "MaritalStatus";
                    cp.DisplayName = "Marital Status";
                    cp.Type = PropertyDataType.StringSingleValue;
                    cp.Length = 100;

                    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();
                }
            }
        }
    }
}

See Also

Tasks

How to: Create User Profiles and Organization Profiles

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 Profile Properties

How to: Create Taxonomical Multivalue Properties

How to: Set Privacy Policies for User Profile Properties