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";


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;

1 comment:

  1. I get this error when trying to assign values to the properties:

    Property Not Editable: This property can not be modified.