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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function ($) { | |
'use strict'; | |
var requestHeaders = { | |
'X-RequestDigest': $("#__REQUESTDIGEST").val(), | |
"accept": "application/json; odata=nometadata", | |
"content-type": "application/json;odata=nometadata" | |
}; | |
var userData = { | |
'accountName': "i:0#.f|membership|user@yourtenant.onmicrosoft.com", | |
'propertyName': 'AboutMe', //can also be used to set custom single value profile properties | |
'propertyValue': 'Value set from REST API!' | |
} | |
$.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); | |
} | |
}); | |
})(jQuery); |
2) Set Mutli Value User Profile property with REST API:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function ($) { | |
'use strict'; | |
var requestHeaders = { | |
'X-RequestDigest': $("#__REQUESTDIGEST").val(), | |
"accept": "application/json; odata=nometadata", | |
"content-type": "application/json;odata=nometadata" | |
}; | |
var userData = { | |
'accountName': "i:0#.f|membership|user@yourtenant.onmicrosoft.com", | |
'propertyName': 'SPS-Skills', //Can also set custom userprofile properties. | |
'propertyValues': ["SharePoint", "Office 365", "Architecture", "Azure"] | |
}; | |
$.ajax({ | |
url: _spPageContextInfo.webAbsoluteUrl + "/_api/SP.UserProfiles.PeopleManager/SetMultiValuedProfileProperty", | |
type: "POST", | |
headers: requestHeaders, | |
data: JSON.stringify(userData), | |
success: function (data) { | |
console.log(data) | |
}, | |
error: function (jqxr, errorCode, errorThrown) { | |
console.log(jqxr.responseText); | |
} | |
}); | |
})(jQuery); |