Getting ModifyUserPropertyByAccountName to work

The documentation for the ModifyUserPropertyByAccountName method of the UserProfileService in the SharePoint Server SDK has a bug.

You'll notice that although there are no errors reported the profile doesn't get updated. The fix is quite simple though and is actually present in the other examples. Setting the IsValueChanged to true does the trick.

Here's how the corrected code looks like.

   1: namespace ModifyUserPropertyByNameSample
   2: {
   3:     class Program
   4:     {
   5:            
   6:         static void Main(string[] args)
   7:         {
   8:             //Instantiate the Web service. 
   9:             UserProfileService userProfileService = 
  10:                 new UserProfileService();
  11:  
  12:             //Set credentials for requests.
  13:             //Use the current user log-on credentials.
  14:             userProfileService.Credentials =
  15:                 System.Net.CredentialCache.DefaultCredentials;
  16:  
  17:             PropertyData[] newdata = new PropertyData[1];
  18:             newdata[0] = new PropertyData();
  19:             
  20:             newdata[0].Name = "FirstName";
  21:             newData[0].IsValueChanged = true;
  22:  
  23:             newdata[0].Values = new ValueData[1];
  24:             newdata[0].Values[0] = new ValueData();
  25:             newdata[0].Values[0].Value = "Mark";
  26:  
  27:             // TODO 
  28:             // Replace "domain\\username" with valid values.
  29:             userProfileService.ModifyUserPropertyByAccountName
  30:                 ("domain\\username", newdata);
  31:  
  32:         }
  33:     }
  34: }

 

For what it's worth I've uploaded a working solution to the MSDN Code Gallery that allows you to view the current profile data and make changes by providing the name/value pair through the query string. You can download the SharePoint Profile Updater here.

Written on February 5, 2008