0

This code works if i have a alert before , when i remove it, the isUserInList always shows false.

Maybe i need to have a return value in CheckIfUserIsInList(); function ?

  var isUserInList = false;

        CheckIfUserIsInList();

 alert(isUserInList); //only works if alert

              if(isUserInList == false){
                  ShowDialog();
               }
               else
               {
                console.log("user dont want information");
               }
     }


  function CheckIfUserIsInList(){

           var clientContext = new SP.ClientContext.get_current();

           var currentUser = clientContext.get_web().get_currentUser();
           var thisList = clientContext.get_web().get_lists().getByTitle('userData');

           var query = new SP.CamlQuery.createAllItemsQuery();
           var listItems = thisList.getItems(query);

           clientContext.load(currentUser);
            clientContext.load(listItems);
            clientContext.executeQueryAsync(Function.createDelegate(this, listSuccess), Function.createDelegate(this, listFailed)); 


           function listSuccess(sender,args){

           var userInfo = currentUser.get_title();

           var listItemEnumerator = listItems.getEnumerator();

             while(listItemEnumerator.moveNext()){

              var olistItem = listItemEnumerator.get_current();
              var userItem = olistItem.get_item("Title");

                if( userInfo == userItem)
                {

                  isUserInList = true;

                }
                else
                {
                  isUserInList = false;
                }
             }

           }


           function listFailed(sender,args){
          console.log('List retrieved error: ' + args.get_message() + '\n' + args.get_stackTrace());
           }

         }
0

Move this

if(isUserInList == false){
    ShowDialog();
}
else
{
    console.log("user dont want information");
}

into listSuccess

function listSuccess(sender,args){

       var userInfo = currentUser.get_title();
       var listItemEnumerator = listItems.getEnumerator();

        while(listItemEnumerator.moveNext()){
          var olistItem = listItemEnumerator.get_current();
          var userItem = olistItem.get_item("Title");

          if(userInfo == userItem)
          {
              if(isUserInList == false){
              ShowDialog();
          }
          else
          {
              console.log("user dont want information");
          }
      }
      else
      {
         console.log("user not in list");
      }
   }
}

Or use somethink like jQuery.deffered to check that the event is actually finished before checking the value: http://api.jquery.com/deferred.promise/

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.