Skip to content

Instantly share code, notes, and snippets.

@vman
Last active February 11, 2016 17:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vman/4701670 to your computer and use it in GitHub Desktop.
Save vman/4701670 to your computer and use it in GitHub Desktop.
(function($){
$(document).ready(function(){
// Ensure that the SP.UserProfiles.js file is loaded before the custom code runs.
SP.SOD.executeOrDelayUntilScriptLoaded(loadUserData, 'SP.UserProfiles.js');
});
var userProfileProperties = [];
function loadUserData(){
//Get Current Context
var clientContext = new SP.ClientContext.get_current();
//Get Instance of People Manager Class
var peopleManager = new SP.UserProfiles.PeopleManager(clientContext);
//Properties to fetch from the User Profile
var profilePropertyNames = ["PreferredName","PictureURL"];
//Domain\Username of the user (If you are on SharePoint Online)
var targetUser = "i:0#.f|membership|vardhaman@yoursite.onmicrosoft.com";
//If you are on On-Premise:
//var targetUser = domain\\username
//Create new instance of UserProfilePropertiesForUser
var userProfilePropertiesForUser = new SP.UserProfiles.UserProfilePropertiesForUser(clientContext, targetUser, profilePropertyNames);
userProfileProperties = peopleManager.getUserProfilePropertiesFor(userProfilePropertiesForUser);
//Execute the Query.
clientContext.load(userProfilePropertiesForUser);
clientContext.executeQueryAsync(onSuccess, onFail);
}
function onSuccess() {
var messageText = "\"Preffered Name\" property is " + userProfileProperties[0];
messageText += "\"PictureURL\" property is " + userProfileProperties[1];
alert(messageText);
}
function onFail(sender, args) {
alert("Error: " + args.get_message());
}
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment