How To: Create custom SharePoint 2010 User Profile properties programatically

Yesterday I talked about how you can create user profiles and assign profile properties programatically. In some instances you would probably want to extend User Profile Service application properties to include your custom properties of different types. You can extend properties with the following user interface (that’s of course assuming your user profiel service application is installed):

1. Navigate to http://[central admin URL]/_admin/ServiceApplications.aspx

2. Select your user profile service application link.

3. Click Manage User Properties

4. Click Create new property as shown below:

create new property

All of the options you see on the page can be set programatically so you can have an import tool create those automatically. Here is how you’d go about provisioning your properties:

1. In your VS 2010 project (you can use console application) ensure you’re referencing the following:

using Microsoft.Office.Server.UserProfiles;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Taxonomy;
using System.Web;
using Microsoft.SharePoint.WebControls;
using System.Web.Profile;
using Microsoft.SharePoint.Portal.WebControls;

2. First we’re going to create a new property section for our custom properties and then create a property itself:

using (SPSite siteColl = new SPSite(socialDataStatsSite))
            {
                SPServiceContext serviceContext = SPServiceContext.GetContext(siteColl);
                UserProfileConfigManager profileConfigManager = new UserProfileConfigManager(serviceContext);
                CorePropertyManager coreProperties = profileConfigManager.ProfilePropertyManager.GetCoreProperties();
                if (coreProperties.GetSectionByName(“MyCustomPropertySection”) == null)
                {
                    CoreProperty MySection = coreProperties.Create(true);
                    MySection.Name = “MyCustomPropertySection”;
                    MySection.DisplayName = Constants.MySectionDisplayName;
                    coreProperties.Add(MySection);
                }
                try
                {

                    List<string> MyCustomProperties = new List<string>();
                    MyCustomProperties.Add(Constants.ContactInfoAddress1);
                    ProfilePropertyManager propertyManager = profileConfigManager.ProfilePropertyManager;

                    foreach (string profileProperty in MyCustomProperties)
                    {
                        if (coreProperties.GetPropertyByName(profileProperty) == null)
                        {
                            CoreProperty propertyInstance = coreProperties.Create(false);
                            propertyInstance.Name = profileProperty.Replace(” “, string.Empty);
                            propertyInstance.Type = “string”;
                            propertyInstance.Length = 50;
                            propertyInstance.Separator = MultiValueSeparator.Semicolon;
                            propertyInstance.DisplayName = profileProperty;
                            propertyInstance.Description = profileProperty;
                            propertyInstance.IsAlias = false;
                            propertyInstance.IsSearchable = true;
                            propertyInstance.Commit();

                            ProfileTypeProperty profileTypeProperty = propertyManager.GetProfileTypeProperties(ProfileType.User).Create(propertyInstance);
                            profileConfigManager.ProfilePropertyManager.GetCoreProperties().Add(propertyInstance);
                            propertyManager.GetProfileTypeProperties(ProfileType.User).Add(profileTypeProperty);
                        }
                    }
                }
                catch (DuplicateEntryException exception)
                {

// …. Handle your own exceptions here :) ….
                }

            }

That’s it! Feel free to experiment with CoreProperty propertyInstance  to see what else you can set through that object. Good luck!

This entry was posted in sharepoint, sharepoint 2010 and tagged , , , , , , . Bookmark the permalink.

Comments are closed.