Showing posts with label User Profiles. Show all posts
Showing posts with label User Profiles. Show all posts

Thursday, March 24, 2011

SharePoint Update User Profile Programatically (SSP User Profiles)

Below is the code snippet to update the user profile with OTB & Custom Properties.
using Microsoft.Office.Server;
using Microsoft.Office.Server.UserProfiles;
using (SPSite site = new SPSite("http://sitecollectionurl"))
{
//User profile manager object holds the complete profile store
UserProfileManager profileManager = new UserProfileManager(ServerContext.GetContext(site));

//UserProfile object holds the acount specific userprofile
UserProfile userProfile = profileManager.GetUserProfile("domain\\administrator");

//OTB property name can be assigned as below using PropertyConstants class
userProfile[PropertyConstants.FirstName].Value = "My first name";

//Custom property value can be assigned as below by hardcoding the property name
userProfile["CustomPropertyName"].Value = "Test Value";

userProfile.Commit();
}

Loop through all the user profiles available in SSP profile store.
UserProfileManager profileManager = new UserProfileManager(ServerContext.GetContext(site));
foreach(UserProfile userProfile in profileManager)
{
if(null != userProfile[PropertyConstants.FirstName].value)
lblFirstName.Text = userProfile[PropertyConstants.FirstName].value;

if(null != userProfile["CustomPropertyName"].value)
lblCustomPropertyValue.Text = userProfile["CustomPropertyName"].value;
}