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.

Getting ModifyUserPropertyByAccountName to work
  • What is interesting is that it still appears to ignore the privacy setting:

    // create sharepoint user profile instance
    UserProfileService MySPService = GetSPUserProfileService();

    // retrieve
    PropertyData[] fieldData = new PropertyData[1];
    fieldData[0] = new PropertyData();
    fieldData[0].Name = fieldName;
    if (privacyValue != Privacy.NotSet)
    {
    fieldData[0].Privacy = privacyValue;
    }
    fieldData[0].Values = new ValueData[1];
    fieldData[0].Values[0] = new ValueData();
    fieldData[0].Values[0].Value = fieldValue;
    fieldData[0].IsValueChanged = true;
    MySPService.ModifyUserPropertyByAccountName(accountName, fieldData);

  • Charftong

    Kudos!! This saved me some valuable time. Good ole MSDN Documentation.

  • mark

    can you convert this code in javascript?
    I been trying lots of code tweaking and still not been able to figure out the right code for this.

    a sample of this is very much appreciated

  • praveen

    hi,

    iam unable to find the source code in the msdn site mentioned by you.Will you please share the code to my mail.

    Thanks,
    Praveen

  • merill

    Hi Praveen,

    Unfortunately I didn’t have a chance to upload the project and don’t seem to have the source code with me. But in general the code in the post above does the trick.

  • praveen

    Thanks merill,

    Iam trying to develop an custom aspx form to update sharepoint user profile properties.

  • I totally agree the standpoint of upstairs, and I believe this will be a trend. I often come this forum , rom here I learn much and know the newest tide! the content here constantly update shoe and I love it! Another I know some websites which often update their contents, you guys should browse if you are free. http://thegy.net

  • Hhe article's content rich variety which make us move for our mood after reading this article. surprise, here you will find what you want! Recently, I found some wedsites which commodity is colorful of fashion. Such as http://www.always11.net that worth you to see. Believe me these websites won’t let you down.

  • Mark S. is definitely on the right track. If you want to get a professional looking email address, Id recommend buying your name domain name, like or
    Jordan Pro Classic
    If its common it might be difficult to get, however, be creative and you can usually find something.

  • Buddha

    Hi Kudos.
    It worked for me perfectly in the case of synchronous call means from ASP or etc.
    It’s not working with asynchronous call means with Silverlight4.

    Please help me!!!