I am currently working on SharePoint 2013 on premise and I am trying to use JavaScript to get the latest 3 "new starters" from a SharePoint list then return information about them such as the Department, Profile Picture and Full name. I have created a custom list and added a person or group field called "name" and this currently displays the users Account.
I am able to pull the required information from the list however I am struggling to pull in all of the required profile information, this is what i currently have:
var web; var list; var list; var mylist; var collListItem; var profilePictureUrl; var accountName; var username; var userProfileProperties = []; var targetUsers = []; $(document).ready(function () { SP.SOD.executeFunc('sp.js', 'SP.ClientContext', getNewStarterList); }) function getNewStarterList() { context = SP.ClientContext.get_current(); web = context.get_web(); list = web.get_lists(); mylist = web.get_lists().getByTitle('New Starters'); var ncamlQuery = new SP.CamlQuery(); ncamlQuery.set_viewXml('<View><RowLimit>3</RowLimit><ViewFields><FieldRef Name=\'Name\' /></ViewFields><Query><OrderBy><FieldRef Name=\'Created\' Ascending=\'True\' /></OrderBy></Query></View>'); collListItem = mylist.getItems(ncamlQuery); context.load(mylist); context.load(collListItem); context.executeQueryAsync( Function.createDelegate(this, newstarterSuccessHandler), Function.createDelegate(this, newstarterErrorHandler)); } function newstarterSuccessHandler() { listItemEnumerator = collListItem.getEnumerator(); while (listItemEnumerator.moveNext()) { oListItem = listItemEnumerator.get_current(); username = oListItem.get_item("Name").get_lookupValue(); targetUsers.push(username); SP.SOD.executeOrDelayUntilScriptLoaded(getNewUserProfile, 'SP.UserProfiles.js'); } } function newstarterErrorHandler(sender, args) { alert('New Starter Error: \nRequest failed. ' + args.get_message() + '\n' + args.get_stackTrace()); } function getNewUserProfile() { for (var x = 0; x < targetUsers.length; x++) { var user = targetUsers[x]; alert(user); } context = SP.ClientContext.get_current(); var peopleManager = new SP.UserProfiles.PeopleManager(context); var profilePropertyNames = ["PreferredName", "PictureURL"]; var userProfilePropertiesForUser = new SP.UserProfiles.UserProfilePropertiesForUser(context, user, profilePropertyNames); userProfileProperties = peopleManager.getUserProfilePropertiesFor(userProfilePropertiesForUser); context.load(userProfilePropertiesForUser); context.executeQueryAsync(onNewStarterSuccess, onNewStarterFail); } function onNewStarterSuccess() { // console.log("succes querying user profiles"); //console.log('User profile property 1:' + userProfileProperties[1]); var nsuserHTML = ""; profilePictureUrl = userProfileProperties[1]; alert("Profile Picture: " + profilePictureUrl); accountName = userProfileProperties[0]; alert("username: " + accountName); } function onNewStarterFail(sender, args) { alert("Error: " + args.get_message()); } I need to be able to pull back the properties to then append to the page layout. I would greatly appreciate any help and advice with how to go about doing this.