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

Related Posts

SharePoint Rest API Handler SharePoint contains a lot of Rest API’s that can be used for many scenario’s. You could use them for example in desktop and windows phone applications...
User automatically removed from SharePoint Group During my current project we received an access request from a user. We did what we normally do, we checked the request and added the user in the appr...
Warm Up Form Based Web Application Normal warm up scripts for SharePoint open a site object or web object and make a request to that specific site. When you use form based authenticatio...
Load modules by default when opening PowerShell At the moment we are working a lot with PowerShell in combination with SharePoint. Over time we created a lot of usable functions that were not availi...
PowerShell Scripts During my current project we are using a lot of PowerShell scripts to administrate the environment and make development easier for us. In this post...
TFS and SharePoint Builds including Automatic deployments – Part 1 Part 1 of this series will instruct how to configure your build server for building SharePoint projects. In Part 2 we will discuss the automatic deplo...

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.