how do I update user profile property in SharePoint 2013
-
10-12-2019 - |
Question
We are using sp2013.
There is a custom user profile property with name: MyProperty (boolean).
The property cannot be edited by user.
I want to update the property value for current user on an application page on click of a button.
Since the property cannot be edited by user, should RWEP?
Solution
An easier approach is to allow the user to update the property and just hide the value from their edit page. Then you don't need RWEP. Just a thought.
OTHER TIPS
Run with Elevated privileges runs the Code in System Account/App pool account. So you will have to make sure that App pool account has "Manage Profile Permission" rights to the User Profile Service. Follow below steps: Go to Central Admin-->Choose Application Management --> Manage Service Applications--> Select the User Profile Service Application (do not open the service application). Click Administrators in the ribbon on top -->Add the App pool account and set the Manage Profiles permission. See this blog for Granting permission to UPS.
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite("http://servername"))//change the hardcoded URL to suit your requirement
{
SPServiceContext serviceContext = SPServiceContext.GetContext(site);
UserProfileManager userProfileMgr = new UserProfileManager(serviceContext);
UserProfile user = userProfileMgr.GetUserProfile(@'domain\username');
user["propertyName"] = "newValue";
user.Commit();
}
});
See this MSDN Blog for Working with UPS using Server Object Model.