Adding profile properties through code

It can occur that you want to add a property to the property collection of the user profile store by using a feature.
If you want to use a feature you would have to create a feature with a FeatureReceiver. In the FeatureReceiver you would have to implement the FeatureActivated and FeatureDeactivating.

The feature.xml file will look like the following:

 <Feature  Id="b6300381-00fe-446a-b6b0-6962344b5d6f"
           Title="UserProfile CV Property"
           Description="Feature that adds a user profile property to the user profile store"
           Creator="Maik van der Gaag"
           ReceiverAssembly="Motion10.SharePoint.UserProfileProperties, Version=, Culture=neutral, PublicKeyToken=7a2aecb48a9c6f26"

In the FeatureActivated we will add the property when it isn’t already added and in the FeatureDeactivating we will delete the property when it is available in the user profile store.

 public override void FeatureActivated(SPFeatureReceiverProperties properties) {
    //get the userprofile manager
    ServerContext context = ServerContext.Current;
    UserProfileManager manager = new UserProfileManager(context);

    //getting the property collection to extend
    PropertyCollection col = manager.Properties;

    if (PropertyExists(col, "NewProperty")) {
       Property p= col.Create(false);
       p.Name = "NewProperty";
       p.DisplayName = "NewProperty";
       p.Type = PropertyDataType.String;
       p.DefaultPrivacy= Privacy.Public;
       p.UserOverridePrivacy = false;

 public override void FeatureDeactivating(SPFeatureReceiverProperties properties) {
    ServerContext context = ServerContext.Current;
    UserProfileManager manager = new UserProfileManager(context);
    PropertyCollection col = manager.Properties;

    if (PropertyExists(col, "NewProperty")) {
       col.RemoveByName(PropertyName, false);

 public static bool PropertyExists(PropertyCollection col, string name){
 bool exists = false;

 var query = from Property p in col
                 where p.Name == name
                 select p;

 if (query.Count() > 0) {
    exists = true;

 return exists;

In the code above you can see that we retrieve the UserProfileManager by using the current ServerContext. On the UserProfileManager you have a PropertyCollection where in we would like to add or delete our property. To create a new property we have to use the Create method on the property collection to create a new property. On the create method you have to give a property whether you would like to add a section or a property. In our example we don’t want to add a section so we choice false.

If you would want to add a property with a section you have to retrieve the property collection with section:

PropertyCollection col = manager.GetPropertiesWithSection();

I hope this article gives you a good insight how to add and remove properties trough code

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.