Microsoft Playground

A Playground for Microsoft Technologies
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:

  1: <Feature  Id="b6300381-00fe-446a-b6b0-6962344b5d6f"
  2:           Title="UserProfile CV Property"
  3:           Description="Feature that adds a user profile property to the user profile store"
  4:           Version="12.0.0.0"
  5:           Hidden="FALSE"
  6:           Scope="Farm"
  7:           Creator="Maik van der Gaag"
  8:           ImageUrl="motion10/FeaturesIcon.png"
  9:           ImageUrlAltText="http://www.motion10.com"
 10:           DefaultResourceFile="core"
 11:           ReceiverAssembly="Motion10.SharePoint.UserProfileProperties, Version=1.0.0.0, Culture=neutral, PublicKeyToken=7a2aecb48a9c6f26"
 12:           ReceiverClass="Motion10.SharePoint.UserProfileProperties.UserProfileProperty"         
 13:           xmlns="http://schemas.microsoft.com/sharepoint/">
 14: </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.

  1: public override void FeatureActivated(SPFeatureReceiverProperties properties) {
  2:    //get the userprofile manager
  3:    ServerContext context = ServerContext.Current;
  4:    UserProfileManager manager = new UserProfileManager(context);
  5:  
  6:    //getting the property collection to extend
  7:    PropertyCollection col = manager.Properties;
  8:  
  9:    if (PropertyExists(col, "NewProperty")) {
 10:       Property p= col.Create(false);
 11:       p.Name = "NewProperty";
 12:       p.DisplayName = "NewProperty";
 13:       p.Type = PropertyDataType.String;
 14:       p.DefaultPrivacy= Privacy.Public;
 15:       p.UserOverridePrivacy = false;
 16:       col.Add(p);
 17:    }
 18: }
 19:  
 20: public override void FeatureDeactivating(SPFeatureReceiverProperties properties) {
 21:    ServerContext context = ServerContext.Current;
 22:    UserProfileManager manager = new UserProfileManager(context);
 23:    PropertyCollection col = manager.Properties;
 24:  
 25:    if (PropertyExists(col, "NewProperty")) {
 26:       col.RemoveByName(PropertyName, false);
 27:    }
 28: }
 29:  
 30: public static bool PropertyExists(PropertyCollection col, string name){
 31: bool exists = false;
 32:  
 33: var query = from Property p in col
 34:                 where p.Name == name
 35:                 select p;
 36:  
 37: if (query.Count() > 0) {
 38:    exists = true;
 39: }
 40:  
 41: return exists;
 42: }

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:

  1: PropertyCollection col = manager.GetPropertiesWithSection();
I hope this article gives you a good insight how to add and remove properties trough code

Comments

No Comments

Leave a Comment

(required) 

(required) 

(optional)

(required) 

  
Enter Code Here: (Required)

Maik van der Gaag

Maik van der Gaag
MCPD