Goal
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.
Code
SPSecurity.RunWithElevatedPrivileges(delegate() { 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); //UserName 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 = "sample@someDomain.com"; //Save the record _userProfile.Commit(); } } } });
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.
