Friday 17 June 2016

SharePoint Online: Write User Profile Properties with REST API

In my previous posts, we saw how to Set user profile properties using JSOM & JavaScript and Set another user's profile properties with CSOM

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.


1) Set Single Value User Profile property with REST API:



2) Set Mutli Value User Profile property with REST API:


Hope you find this useful!

2 comments:

Imanol said...

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.

Vardhaman Deshpande said...

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