Thursday, January 19, 2012

SharePoint 2010 - Update user's infomation using UserProfileManager programmatically.


To update a user's information using C#. In the below given sample i am changing the Email(WorkEmail) of a user.

Important facts

  • User trying to update a profile should be updating own record or should be an "Admin in User Profile Service". If the user is not an Admin in User Profile Service the error will come "You must have manage user profiles administrator rights to use administrator mode.".
  • In below given code I am using true for IgnoreUserPrivacy. Default it should be false but i also wanted to make this work.
    UserProfileManager(SPServiceContext.Current , true)
  • Most Important : The user i am using to run this is an Admin in User Profile Service. So the code is working fine.


    using (SPSite _site = new SPSite(SPContext.Current.Site.Url))
        using (SPWeb _web = _site.OpenWeb())
            //Gets the profile manager object
            UserProfileManager _profileManager = new UserProfileManager(SPServiceContext.Current , true);
            string _userName = @"MyMachine\User";

            //Gets the profile of a User
            UserProfile _userProfile = _profileManager.GetUserProfile(_userName);
            if (_userProfile != null)
                //Change the Email value
                ((UserProfileValueCollection)_userProfile["WorkEmail"]).Value = "";
                //Save the record

This code works absolutely fine. For the developers who are getting issue like "You must have manage user profiles administrator rights to use administrator mode." please click the error to visit the solution.


  1. You're using a RunWithElevatedPrivileges block, and then you create a UserProfileManager object and pass SPServiceContext.Current as parameter, which makes your ElevatedPrivileges completely irrelevant because it won't use the elevated context you're trying to create, but the context of the user running the code.

    It works because your user is a User Profile Administrator, but it doesn't work if you run this code as a user who is not a User Profile Administrator.

    That is the reason why you would try the RunWithElevatedPrivileges block, but the way you use it in your example here, is not correct (and it doesn't work for the UserProfileManager object anyway).

    1. Hello there,

      I have been using the same code in one of my web part which is running completely fine! You might be correct in your explanation!

      Thanks once again for reading my blog.
      Maulik Dhorajia