Please note this code snippet has been tested on SharePoint 2010 using Visual Studio 2012. It should work fine with SharePoint 2013.
If you are working on multi-lingual or support multiple languages in your SharePoint application, there might be at times, you may need to access localized version of SharePoint User Profile Properties.
As many of you may aware, SharePoint User Profile system allows an administrator to specify localized version of labels while creating User Profile Properties. I may have spent day or two and one or more sleepless night to figure out how to localize User Profile Properties using OOB approach before stumbling into localized display name option in User Profile Administration page and where Microsoft may have stored these localized data. As it turned out, these localized labels are stored in the User Profile database unlike other SharePoint localized technologies like .NET Resource files stored in the Resources directory in SharePoint Root or App_GlobalResources in IIS virtual directory.
As demonstrated below, you can manage localized labels of User Profile Properties in the Central Administration. Please note that you must enable language settings for specified lanaguages in Central Administration site to add localized labels for User Profile properties.
As you can see, Localized values of User Profile Properties are stored in the User Profile Service Application’s Profile Database and PropertyListLoc Table.
Let me first point out, Accessing these localized display labels are not straightforward. Microsoft.Office.Server.UserProfiles namespace has CoreProperty class to represent the definition of basic User profile property which is common across all the User Profile Properties. This class represents most values required to define User Profile Properties including Name, Display Name, IsAlias, IsMultiValued, IsSearchable etc. To retrieve localized label of User Profile Properties, what we are interested is => CoreProperty class’s CoreProperty.DisplayNameLocalized collection.
As happens often with SharePoint, there are multiple ways to access User Profile Properties through Server Side Object Model and depending on requirements, one option might perform better than other. In this article, I will demonstrate two different ways to access Localized Label of User Profile properties.
Option 1 => Accessing User Profile Properties CoreProperty Class using CorePropertyManager, Much Better Performance
Once you get access to the UserProfilePropertyManager, you can directly access CorePropertyManager which would allow you to search all the Core Properties for specific User Profile Property and returns object representing CoreProperty class for specific user profile property. This approach allows you to avoid iterating through all the User Profile Properties and yields much better performance.
/// <summary> /// Get the localized label of User Profile Properties /// Option 1 => Get the specific property to find the label (much better performance) /// </summary> /// <param name="userProfilePropertyName"></param> /// <returns></returns> private string getLocalizedUserProfilePropertiesLabel_FasterOption(string userProfilePropertyName) { string localizedLabel = string.Empty; //Get the handle of User Profile Service Application for current site (web application) SPSite site = SPContext.Current.Site; SPServiceContext context = SPServiceContext.GetContext(site); UserProfileConfigManager upcm = new UserProfileConfigManager(context); //Access the User Profile Property manager core properties ProfilePropertyManager ppm = upcm.ProfilePropertyManager; CorePropertyManager cpm = ppm.GetCoreProperties(); //Get the core property for user profile property and get the localized value CoreProperty cp = cpm.GetPropertyByName(userProfilePropertyName); if (cp != null) { localizedLabel = cp.DisplayNameLocalized[System.Globalization.CultureInfo.CurrentUICulture.LCID]; } return localizedLabel; }
Option 2 => Accessing User Profile Properties CoreProperty Class using ProfileSubtypePropertyManager, Slower Looping Through Option
Another option is loop through all the User Profile Properties in the ProfileSubtypePropertyManager. All the User Profile Properties are categorized by Profile Subtypes. This option forces you to loop through User Profile Properties collection to access specified User Profile Property class and object representing CoreProperty class for specific user profile property.
/// <summary> /// Get the localized label of User Profile Properties /// Option 2 => Loop through all the user profile properties to find the label.. /// </summary> /// <param name="userProfilePropertyName"></param> /// <returns></returns> private string getLocalizedUserProfilePropertiesLabel_SlowerOption(string userProfilePropertyName) { string localizedLabel = string.Empty; //Get the handle of User Profile Service Application for current site (web application) SPSite site = SPContext.Current.Site; SPServiceContext context = SPServiceContext.GetContext(site); ProfileSubtypeManager psm = ProfileSubtypeManager.Get(context); ProfileSubtype ps = psm.GetProfileSubtype(ProfileSubtypeManager.GetDefaultProfileName(ProfileType.User)); //Get the properties by section ProfileSubtypePropertyManager pspm = ps.Properties; foreach (ProfileSubtypeProperty profileSubtypeProperty in pspm.PropertiesWithSection) { if (profileSubtypeProperty.Name.Trim().ToUpper() == userProfilePropertyName.Trim().ToUpper()) { //Get the core property for user profile property and get the localized value CoreProperty coreproperty = profileSubtypeProperty.CoreProperty; localizedLabel = coreproperty.DisplayNameLocalized[System.Globalization.CultureInfo.CurrentUICulture.LCID]; break; } } return localizedLabel; }
Enjoy!!!!
Filed under: SharePoint 2010, SP2010 Code Snippets
