In my last post, I was showing how you can retrieve existing properties from User Profiles in SharePoint 2010. In this example I’ll show you how you can add values to user profiles on the fly.
Suppose you want to add a new value to the skills property
in this case you see that I have a “Test” tag and “my term” tag – here is how we add “my term” to the current user Skills property:
using Microsoft.Office.Server.UserProfiles;
public void AddSkill()
{
string socialDataStatsSite = SPContext.Current.Site.Url;
using (SPSite siteColl = new SPSite(socialDataStatsSite))
{
SPServiceContext serviceContext = SPServiceContext.GetContext(siteColl);UserProfile userProfile = ProfileLoader.GetProfileLoader().GetUserProfile();
if (userProfile[PropertyConstants.Skills] != null)
{
userProfile[PropertyConstants.Skills].Add(“my term”);
SPContext.Current.Web.AllowUnsafeUpdates = true;
// or userProfile[PropertyConstants.Skills].AddTaxonomyTerm( … );
userProfile.Commit();
}
}
}
If you add a term that has been added before – no duplicate will be created. Also as a result of your add operation you will receive a count of current term items in the property.
Enjoy!