Skip to content

Instantly share code, notes, and snippets.

@vman
Last active July 18, 2018 13:40
Show Gist options
  • Select an option

  • Save vman/b0ecc1f8c7f3c1e6499c to your computer and use it in GitHub Desktop.

Select an option

Save vman/b0ecc1f8c7f3c1e6499c to your computer and use it in GitHub Desktop.
(function(){
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|[email protected]"
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());
});
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment