0

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.

1 Answer 1

0

You have to use the accountName for pulling data from the userprofile. You are now using .get_item("Name").get_lookupValue() , that gives you the display name. And I don't think there is SP.Userprofiles.js key in the _v_dictSod, I have always used the key userprofile. Could be SharePoint online thing.

I just pulled your code together some just for a test ( usually I nest it beyond reason when testing in the console), but should be clear enough to see how it could work. Note that you can "batch up" calls, like I do to the userprofile there, instead of 3 calls, you can make one.

Example:

SP.SOD.loadMultiple(["sp.js", "userprofile"], getNewStarterList); function getNewStarterList() { var context = SP.ClientContext.get_current(); 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>'); var collListItems = context.get_web().get_lists().getByTitle("New Starters").getItems(ncamlQuery); context.load(collListItems); context.executeQueryAsync(function() { var targetUsers = []; var siteUserList = context.get_web().get_siteUserInfoList(); collListItems.get_data().forEach(function(item,i) { if( item.get_item("Name") != null) { var userNameItem = siteUserList.getItemById( item.get_item("Name").get_lookupId() ); targetUsers.push({ "lookupValue" : item.get_item("Name").get_lookupValue(), "userNameItem": userNameItem }); context.load( userNameItem, "UserName"); } }); context.executeQueryAsync(function() { var profilePropertyNames = ["PreferredName", "PictureURL"]; targetUsers.forEach(function(ele, i) { var userProfilePropertiesForUser = new SP.UserProfiles.UserProfilePropertiesForUser(context, "i:0#.f|membership|"+ ele.userNameItem.get_item("UserName"), profilePropertyNames); ele.properties = new SP.UserProfiles.PeopleManager(context).getUserProfilePropertiesFor(userProfilePropertiesForUser); context.load(userProfilePropertiesForUser); }); context.executeQueryAsync(function() { targetUsers.forEach(function(ele, i) { var nsuserHTML = ""; var profilePictureUrl = ele.properties[1]; console.log( profilePictureUrl ); }); }, onFail); }, onFail); }, onFail); } function onFail(sender, args) { console && console.log("Error: " + args.get_message()); } 

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.