While working on user profile service I got the requirement
to create custom property through coding for one of my client. I have found the
solution to create custom property from Ahmedmandany blog. To create the property, we have to get the current service context by passing the site object. From the service
context object create new UserProfileConfigManager object. By using
UserProfileConfiguaration object we can get the CoreProperties in the service
as shown the code below.
SPServiceContext
currentServiceContext = SPServiceContext.GetContext(SiteCollection);
//Here SiteCollection
is SPSite object for current site
UserProfileConfigManager
userProfileConfigManager = new UserProfileConfigManager(currentServiceContext);
CorePropertyManager
usreProfileCoreProperties =
userProfileConfigManager.ProfilePropertyManager.GetCoreProperties();
Create profileProperty list and with string type objects. Insert data into the list as shown below,
List<string> myCustomProperties = new List<string>();
myCustomProperties.Add("My Custom Property");
We can get the ProfilePropertyManager object from
ProfileConfigManager.
ProfilePropertyManager propertyManager = profileConfigManager.ProfilePropertyManager;
Check the property is exits or not in CoreProperties. If
not exists, add property to the core properties. Add PropertyInstance, ProfileTypeProperty
and ProfileSubtypePropertyManager objects, update all the properties as per the
requirements.
CoreProperty coreProperty
= coreProperties.Create(false);
coreProperty.Name =
profileProperty.Replace(" ", string.Empty);
coreProperty.Type =
PropertyDataType.String;
coreProperty.Length = 4000;
coreProperty.DisplayName
= "My Custom Property";
coreProperty.Description = "My Custom Property" ;
coreProperty.IsAlias=false;
coreProperty.Commit();
ProfileTypePropertyManager profileTypePropertyManager
= propertyManager.GetProfileTypeProperties(ProfileType.User);
ProfileTypeProperty profileTypeProperty
= profileTypePropertyManager.Create(coreProperty);
profileTypeProperty.IsVisibleOnViewer
=true;
profileTypeProperty.IsVisibleOnEditor = true;
profileTypePropertyManager.Add(profileTypeProperty);
ProfileSubtypeManager profileSubTypeManager =
ProfileSubtypeManager.Get(currentServiceContext);
ProfileSubtype profileSubtype = profileSubTypeManager.GetProfileSubtype (ProfileSubtypeManager.GetDefaultProfileName(ProfileType.User));
ProfileSubtypePropertyManager profileSubtypePropertyManager = profileSubtype.Properties;
ProfileSubtypeProperty
profileSubtypeProperty = profileSubtypePropertyManager.Create(profileTypeProperty);
profileSubtypeProperty.IsUserEditable = true;
profileSubtypePropertyManager.Add(profileSubtypeProperty);