title: SharePoint COM retrieving list data from site and sub-site on the same page
body: <p>I am trying to access list data from site and several levels of sub-sites in one page.
  I have searched the web and tried to apply few solutions found here on Stack Overflow but to no avail.
  This is the simplified code I am using.</p>
  
  <pre><code>// parent-level 
  var clientCtx = new SP.ClientContext.get_current(); 
  
  getListItems(listName, camlView,fields, clientCtx);
  
  //sub-site level
  clientCtx = new SP.ClientContext(subSiteUrl);
  getListItems((listName, camlView,fields, clientCtx);
  </code></pre>
  
  <p>This code works for the parent site but not for sub-sites.
  The client context passed inside getListItems function for sub-site contains the right URL and seems to instantiate all objects: web, list, listItems, etc., but fails to enumerate list items with the "Unknown error". The same thing happens for both sub-sites i have tried.</p>
  
  <pre><code>function getListItems(listName, queryString, fields, clientCtx)
  {
  web = clientCtx.get_web();
  var list = web.get_lists().getByTitle(listName);       
  var query = new SP.CamlQuery();
  query.set_viewXml(queryString);
  var listItems = list.getItems(query);
  
  query.set_viewXml(queryString);
  var listItems = list.getItems(query);
  var fieldList = fields.join(",");
  
  clientCtx.load(listItems); 
  
  clientCtx.executeQueryAsync(function () {
  var resultItems = [];
  var listItemEnumerator = listItems.getEnumerator();
  while (listItemEnumerator.moveNext()) {
       var listItem = listItemEnumerator.get_current();                          
  
       var fieldValues = listItem.get_fieldValues();
       var resultItem = {};                           
       for (var i = 0; i &lt; fields.length; i++) {
            resultItem[fields[i]] = listItem.get_item(fields[i]);
       }
       resultItems.push(resultItem);
  }
  return resultItems;
  }
  ...
  </code></pre>
  
  <p>}</p>