I'm pulling user data, saving that to an array, and then using that array to pull in additional values from PeopleManager. The problem is (at least so far) my array has a blank value in it that I can't seem to get rid of.

var targetUsers = [];

function getCurrentUser() {
var clientContext = new SP.ClientContext.get_current();
var collGroup = clientContext.get_web().get_siteGroups();
var oGroup = collGroup.getById(12);
collUser = oGroup.get_users();
clientContext.load(collUser, 'Include(LoginName)');

clientContext.executeQueryAsync(onSucceed, onFail);

function onSucceed() {
var userEnumerator = collUser.getEnumerator();
while (userEnumerator.moveNext()) {
     var oUser = userEnumerator.get_current();
     targetUsers += oUser.get_loginName() + ",";
     targetUsers = targetUsers.replace('undefined', ""); //gets rid of my undefined value
     targetUsers = targetUsers.split(',');  //creates my array object

 [object array]
   > [0...99]
     > [90...99]
      > 90 ... LOGINVALUE
          ... etc
       99 ... ""

NOTE 99 returns this ""

I'm trying to get rid of that. Not sure where it's coming from.

Here's the code I started out with (http://www.vrdmn.com/2013/02/sharepoint-2013-working-with-user.html):


// Ensure that the SP.UserProfiles.js file is loaded before the custom 
code     runs.
(loadUserData, 'SP.UserProfiles.js');  

  var userProfileProperties = [];

  //Array containing domain\usernames of multiple users. You can get 
  the       usersnames any way you want.
  var targerUsers =    
  demouser1@yoursite.onmicrosoft.com"];  //This is the array I'm trying
  to   populate

  //If you are on On-Premise:
  //var targerUsers = ["domain\\username","domain\\demouser1"];

  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);

  //Property to fetch from the User Profile
  var propertyName = "PreferredName";       

  for(var i=0;i<targerUsers.length;i++){

  //Create new instance of UserProfileProperty
  userProfileProperties[i] = peopleManager.getUserProfilePropertyFor
  (targerUsers[i], propertyName);

  //Execute the Query. (No load method necessary)
  clientContext.executeQueryAsync(onSuccess, onFail);


  function onSuccess() {
  var messageText = "";

  for(var i=0;i<userProfileProperties.length;i++){
  messageText += "\"Preffered Name\" property is " + userProfileProperties


  function onFail(sender, args) {
  alert("Error: " + args.get_message());


You are declaring targetUsers as an Array:

var targetUsers = [];

Then appending Strings:

 targetUsers += oUser.get_loginName() + ",";

You're doing a bit wrong. Replace the following code:

while (userEnumerator.moveNext()) {
     var oUser = userEnumerator.get_current();
     targetUsers += oUser.get_loginName() + ",";


while (userEnumerator.moveNext()) {
     var oUser = userEnumerator.get_current();

As Danny already said, you create the targetUsers variable as an array and then use it as a string which you later convert into an array again. If you need an array, then work with an array. If you need a string, work with a string. Your scenario does not seem to require you to switch from one type to another.

To remove all undefined values from the array, do this:

for (var index = 0; index < targetUsers.length; index++) {
    if (targetUsers[index] === undefined) {
        targetUsers.splice(index, 1);
  • thanks, Mihail. Yes, I pasted the code I started with. The targetusers started out as an array I'm assuming it needs to be. Original code had a few users but I needed to populate several from a group and still have it work – JackSki Jun 16 '16 at 10:28
  • Mihail, just tried your change "targetUsers.push(oUser.get_loginName());' and it worked. – JackSki Jun 16 '16 at 10:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.