Getting ModifyUserPropertyByAccountName to work
Posted by | Filed under SharePoint
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.
June 24th, 2008 at 7:49 am
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);
June 24th, 2009 at 4:31 am
Kudos!! This saved me some valuable time. Good ole MSDN Documentation.
October 13th, 2009 at 5:04 am
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
October 26th, 2009 at 5:27 pm
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
October 26th, 2009 at 5:45 pm
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.
October 26th, 2009 at 6:22 pm
Thanks merill,
Iam trying to develop an custom aspx form to update sharepoint user profile properties.