Is there a REST API for updating User Profiles?
I've found the one to read them, but can't seem to work out how to update them.
User Profile Properties With REST API–SharePoint Office 365.
Thanks.
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It only takes a minute to sign up.
Sign up to join this communityIs there a REST API for updating User Profiles?
I've found the one to read them, but can't seem to work out how to update them.
User Profile Properties With REST API–SharePoint Office 365.
Thanks.
Update SingleValueProperty
function updateSingleValue(){
var Headers = {
'X-RequestDigest': $("#__REQUESTDIGEST").val(),
"accept": "application/json; odata=nometadata",
"content-type": "application/json;odata=nometadata"
};
var aboutData = {
'accountName': "i:0#.f|membership|user@yourtenant.onmicrosoft.com",
'propertyName': 'AboutMe', //can also be used to set custom single value profile properties
'propertyValue': 'A tech savvy and computer geek which lives by earning bread on computers, passes time by watching/sitcoms and playing games on computers, also a loving husband, father, brother, son and friend and most importantly trying to be human being :)'
}
$.ajax({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/SP.UserProfiles.PeopleManager/SetSingleValueProfileProperty",
type: "POST",
headers: Headers,
data: JSON.stringify(aboutData),
success:profileUpdateSuccess ,
error: profileUpdateFail
});
function profileUpdateSuccess(response){
console.log(response);
}
function profileUpdateFail(jqxr, errorCode, errorThrown){
console.log(jqxr);
}
}
Updating Multi Value property
function updateMultiValue(){
var Headers = {
'X-RequestDigest': $("#__REQUESTDIGEST").val(),
"accept": "application/json; odata=nometadata",
"content-type": "application/json;odata=nometadata"
};
var aboutData = {
'accountName': "i:0#.f|membership|user@yourtenant.onmicrosoft.com",
'propertyName': 'SPS-Skills', //can also be used to set custom single value profile properties
'propertyValue': ["SharePoint", "Office 365", "Node JS", "Ionic", "Gsuite"]
}
$.ajax({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/SP.UserProfiles.PeopleManager/SetMultiValuedProfileProperty",
type: "POST",
headers: Headers,
data: JSON.stringify(aboutData),
success:profileUpdateMultiSuccess ,
error: profileUpdateMultiFail
});
}
function profileUpdateMultiSuccess(response){
console.log(response);
}
function profileUpdateMultiFail(jqxr, errorCode, errorThrown){
console.log(jqxr);
}
Hope this helps...Happy coding..!!!
You can also set user profile properties using JSOM:
SP.SOD.executeFunc("sp.js", "SP.ClientContext", function(){
SP.SOD.registerSod("sp.userprofiles.js", SP.Utilities.Utility.getLayoutsPageUrl("sp.userprofiles.js"));
SP.SOD.executeFunc("sp.userprofiles.js", "SP.UserProfiles.PeopleManager", SetCurrentUserProperties);
});
var userProfileProperties;
function SetCurrentUserProperties(){
//Get Current Context
var clientContext = SP.ClientContext.get_current();
//Get Instance of People Manager Class
var peopleManager = new SP.UserProfiles.PeopleManager(clientContext);
//Get properties of the current user
userProfileProperties = peopleManager.getMyProperties();
//Get only the accountname instead of all the properties.
clientContext.load(userProfileProperties, "AccountName");
//Execute the Query.
clientContext.executeQueryAsync(function(){
//Get the account name of the current user. It will be in the following format: "i:0#.f|membership|username@yoursite.onmicrosoft.com"
var currentUserAccountName = userProfileProperties.get_accountName();
//Set a single value property
peopleManager.setSingleValueProfileProperty(currentUserAccountName, "AboutMe", "Value updated from JSOM!");
//Set a multivalue property
var multipleValues = ["SharePoint", "Office 365", "Architecture"];
peopleManager.setMultiValuedProfileProperty(currentUserAccountName, "SPS-Skills", multipleValues);
//Execute the Query.
clientContext.executeQueryAsync(function(){
console.log("properties updated!");
},
function(sender,args){
//On Error
console.log(args.get_message());
});
}, function(sender,args){
//On Error
console.log(args.get_message());
});
}
Reference: