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:


// Get the property from the UserProfileManager instance
Property property = userProfileManager.PropertiesWithSection.GetPropertyByName("MyExistingProperty")
// Remove old choice items
property.ChoiceList.Remove("Old Choice 1");
property.ChoiceList.Remove("Old Choice 2");
// Add new choice items
property.ChoiceList.Add("New Choice 1");
property.ChoiceList.Add("New Choice 2");
// Save the property
property.Commit();

This will work:


// Get the property from the UserProfileManager instance
Property property = userProfileManager.PropertiesWithSection.GetPropertyByName("MyExistingProperty")
// Remove old choice items
property.ChoiceList.Remove("Old Choice 1");
property.ChoiceList.Remove("Old Choice 2");
// Add new choice items
property.ChoiceList.Add("New Choice 1");
property.ChoiceList.Add("New Choice 2");
// Increase MaximumShown by the amount of properties that were added (ignore the number of properties removed)
property.MaximumShown += 2;
// Save the property
property.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 *

*