title: Retrieve list items from multiple lists with urls stored in an array
body: <p>How do i get list items from different lists in SharePoint using javascript. Given that all my list are stored in an array. And I need to loop through the array and execute similar functions on each list. </p>
  
  <pre><code>function initializePage() {
  
  listcollections.forEach(function (value, index) { // listcollections is the array that contains the list url for different lists
  
      var listtitle = value.listTitle;
      var siteUrl = value.siteURL;
  
  
      getItemsWithCaml(siteUrl, listtitle,
      function (camlItems) {
  
  
          var listItemEnumerator = camlItems.getEnumerator();
          while (listItemEnumerator.moveNext()) {
  
              var EventsItem = new Events();
              var listItem = listItemEnumerator.get_current();
  
              EventsItem.eventTitle = listItem.get_item('Title');
              EventsItem.eventDate = listItem.get_item('EventDate');
              EventsItem.eventId = listItem.get_id();
              EventsItem.eventSite = siteUrl;
              EventsItem.eventList = listtitle;
  
              EventsCollection.push(EventsItem);
  
  
          }
      },
          function (sender, args) {
              alert('An error occurred while retrieving list items:' + args.get_message());
          });       
  })
  </code></pre>
  
  <p>}; </p>
  
  <pre><code>function getItemsWithCaml(siteUrl, listtitle, header, success, error) 
  </code></pre>
  
  <p>{</p>
  
  <pre><code>var hostWebContext = new SP.ClientContext(siteUrl);
  
  var list = hostWebContext.get_web().get_lists().getByTitle(listtitle);
  
  var caml = new SP.CamlQuery();
  
  //Create the CAML that will return only items expiring today or later
  caml.set_viewXml("&lt;View&gt;&lt;Query&gt;&lt;Where&gt;&lt;Geq&gt;&lt;FieldRef Name=\'Expires\'/&gt;&lt;Value Type=\'DateTime\'&gt;&lt;Today /&gt;&lt;/Value&gt;&lt;/Geq&gt;&lt;/Where&gt; &lt;/Query&gt;&lt;/View&gt;");
  var camlItems = list.getItems(caml);
  hostWebContext.load(camlItems);
  hostWebContext.executeQueryAsync(
          function () {
  
              success(camlItems);
          },
          error
      );
  </code></pre>
  
  <p>};</p>
  
  <p>//need to execute certain functions to format each list item</p>
  
  <p>// I am not able to retrieve all list items in a single variable to be able to display data from all lists together</p>