In this post, I will show you how to set the current user's profile properties using the JavaScript Client Object Model (JSOM). Please refer to the 2 posts I have mentioned for any additional details around this.
Right now this functionality is only available in SharePoint Online/Office 365. If you want this functionality to come to SharePoint 2013 On-Premises, please create or up-vote on the feedback here: https://officespdev.uservoice.com/
Right now this functionality is only available in SharePoint Online/Office 365. If you want this functionality to come to SharePoint 2013 On-Premises, please create or up-vote on the feedback here: https://officespdev.uservoice.com/
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(){ | |
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()); | |
}); | |
} | |
})(); |