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

2 comments:

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

    Property Not Editable: This property can not be modified.

    ReplyDelete
  2. Only the editable properties can be updated through UI or API. I think you are trying to update a property which is non editable. You can make a property editable through UI by following the below steps...

    Central Administration >> SSP >> User profiles and properties (under User Profiles and My Sites section) >> View profile properties (under User Profile Properties section) >> Edit Specific Property >> Edit Settings Section >> Make sure the respective radio button is selected to edit the property

    ReplyDelete