Now, here are some code snippets I have put together to set SharePoint User Profile properties with the SharePoint REST API
- This code only works with SharePoint Online at this time.
- Can be used to set single value as well as multi value user profile properties.
- Can be used to set default (OOB) as well as custom user profile properties.
2 comments:
Hi,
Congrats for the post! It´s very usefull.
When I tried to update "Manager" field, throws an exception. I am a global admin of the tenant and I dont like to put the property editable for the user.
"{"odata.error":{"code":"-1, Microsoft.Office.Server.UserProfiles.PropertyNotEditableException","message":{"lang":"es-ES","value":"Propiedad no editable: esta propiedad solo puede modificarla un administrador."}}}
This is the code:
function updateManager() {
var selectedUser = $("#usuarios").find(":selected")[0].value;
var newManager = $("#newManagers").find(":selected")[0].value;
var requestHeaders = {
'X-RequestDigest': $("#__REQUESTDIGEST").val(),
"accept": "application/json; odata=nometadata",
"content-type": "application/json;odata=nometadata"
};
var userData = {
'accountName': "i:0#.f|membership|" + selectedUser ,
'propertyName': 'Manager', //can also be used to set custom single value profile properties
'propertyValue': "i:0#.f|membership|" + newManager
}
$.ajax({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/SP.UserProfiles.PeopleManager/SetSingleValueProfileProperty",
type: "POST",
headers: requestHeaders,
data: JSON.stringify(userData),
success: function (data) {
console.log(data)
},
error: function (jqxr, errorCode, errorThrown) {
console.log(jqxr.responseText);
}
});
}
any idea? how can I update manager propety?
Thanks in advance.
Hi Imanol,
I am afraid you cannot set the property from JSOM unless you set the property as editable by the user.
One workaround could be that you set the property as editable by the user but hide it from the delve profile by unchecking "Show on the Edit Details page". But bear in mind this will only hide the property but the user will still be able to update it through other ways such as code etc.
Thanks
Post a Comment