title: SharePoint - Display Templates - Getting Every Item in All Lists in Site
body: <p><strong>I'm trying to get every single item from all lists within a SharePoint site.</strong>  All of the docs and answered questions I've found in order to do this usually explain how to get all lists or all items from <strong>one</strong> list, but not every item in all lists within a site context.  </p>
  
  <p>I have my code below and I am able to get all lists fine, my biggest struggle is getting the items and not only from the last list (for some reason, it kept on doing this as I tested in the console - this version would probably just produce null errors instead since I made changes from before).</p>
  
  <pre><code>var allInfo = "";
  
          SP.SOD.executeFunc('sp.js', 'SP.ClientContext', getAllListsAndItems);
  
          function getAllListsAndItems() {
  
              var context = new SP.ClientContext('https://mysites.sprcompanies.com/personal/cnorman/charpractice/');
              var web = context.get_web();
              var lists = web.get_lists();
  
              context.load(lists);
  
              context.executeQueryAsync(onQuerySucceeded, onQueryFailed);
  
              function onQuerySucceeded(sender, args) {            
  
                  var listEnumerator = lists.getEnumerator();
  
                  while (listEnumerator.moveNext()) {
                      var list = listEnumerator.get_current();                    
                      allInfo += " List: " + list.get_title() + "\n";
  
                      if (list.get_itemCount() &gt; 0) {
  
                          var query = new SP.CamlQuery();
                          query.set_viewXml('&lt;View&gt;&lt;/View&gt;');
                          var items = list.getItems(query);
  
                          context.load(items);
  
                          context.executeQueryAsync(onSuccess, onFail);
  
                          function onSuccess(sender, args) {
  
                              var itemsEnumerator = items.getEnumerator();
  
                              while (itemsEnumerator.moveNext()) {
                                  var item = itemsEnumerator.get_current(); 
                              }      
  
                          }
  
                          function onFail(sender, args) {
                              console.log('Request on items failed. ' + args.get_message() + '\n' + args.get_stackTrace());
                          }
                      }      
                  }
  
                  console.log(allInfo);
              }
  
              function onQueryFailed(sender, args) {
                  console.log('Request on lists failed. ' + args.get_message() + '\n' + args.get_stackTrace());
              } 
          }
  </code></pre>
  
  <p>I know the general problem area is here:</p>
  
  <pre><code>var itemsEnumerator = items.getEnumerator();
  
      while (itemsEnumerator.moveNext()) {
          var item = itemsEnumerator.get_current(); 
      } 
  </code></pre>
  
  <p>I originally had it append to <code>allInfo</code>, but all it does is produce 'not initialized' errors.  I first thought that I just wasn't loading the items properly, but after testing it in the console, it does display the item collection objects so that's why I think it has something to do with the above.</p>
  
  <p>Couldn't I just use a <code>for</code> loop to cycle through the items instead?  I only need the titles of each item.  I tried a <code>for</code> and a <code>for in</code>, but it again results in errors.  So it's really how I'm accessing each item (using the wrong properties).  Thank you in advance!</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>So I put this in the item <code>onSuccess</code> block instead:</p>
  
  <pre><code>if (items.get_item("Title") == null) {
      items.get_data().forEach(function(item) {     
          console.log(item.get_item('URL').get_description()); 
      }); 
  }
  
  else {
      items.get_data().forEach(function(item) {
          console.log(item.get_item('Title')); 
      });
  }  
  </code></pre>
  
  <p>Both would get the 'title' of an item whether it's a regular item or a link item - the problem is that it only get the items of the last list and repeats those multiple times instead of going through every list.   </p>