Adding choices to existing UserProfile properties

When editing ChoiceList items of existing UserProfile properties you also need to increment the MaximumShown property. Otherwise the changes (neiter removal nor addition) will be saved when calling Commit().

This won’t work:

01// Get the property from the UserProfileManager instance
02Property property = userProfileManager.PropertiesWithSection.GetPropertyByName("MyExistingProperty")
03// Remove old choice items
04property.ChoiceList.Remove("Old Choice 1");
05property.ChoiceList.Remove("Old Choice 2");
06// Add new choice items
07property.ChoiceList.Add("New Choice 1");
08property.ChoiceList.Add("New Choice 2");
09// Save the property
10property.Commit();

This will work:

01// Get the property from the UserProfileManager instance
02Property property = userProfileManager.PropertiesWithSection.GetPropertyByName("MyExistingProperty")
03// Remove old choice items
04property.ChoiceList.Remove("Old Choice 1");
05property.ChoiceList.Remove("Old Choice 2");
06// Add new choice items
07property.ChoiceList.Add("New Choice 1");
08property.ChoiceList.Add("New Choice 2");
09// Increase MaximumShown by the amount of properties that were added (ignore the number of properties removed)
10property.MaximumShown += 2;
11// Save the property
12property.Commit();

See also: http://blogs.msdn.com/nishand/archive/2007/05/20/updating-userprofile-property-of-type-choicelist.aspx

This entry was posted in .NET and tagged , , , . Bookmark the permalink.

Leave a Reply

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

*