1 minute read

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"
           Version="12.0.0.0"
           Hidden="FALSE"
           Scope="Farm"
           Creator="Maik van der Gaag"
           ImageUrl="motion10/FeaturesIcon.png"
           ImageUrlAltText="http://www.motion10.com"
           DefaultResourceFile="core"
           ReceiverAssembly="Motion10.SharePoint.UserProfileProperties, Version=1.0.0.0, Culture=neutral, PublicKeyToken=7a2aecb48a9c6f26"
           ReceiverClass="Motion10.SharePoint.UserProfileProperties.UserProfileProperty"
           xmlns="http://schemas.microsoft.com/sharepoint/">
 </Feature> 

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;
       col.Add(p);
    }
 }

 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;
 }