title: Running nested queries in Sharepoint
body: <p>Is it possible to run nested queries in Sharepoint? It seems like the asynchronous demand of Sharepoint stops me from doing what I'd like. Basically I have two lists. One list, Tickets, describes a particular event while the other, Updates, lists updates to those events. The primary key in Tickets links to a foreign key in Updates. I am trying to create a dashboard to show the Tickets along with their updates.</p>
  
  <p>The following code creates the table fine and adds the correct entries from the Tickets table but does not add the information from the Updates table. I'm assuming this is because of the asynchronous nature of Sharepoint's list query and am unable to find a way around it. Using a JQuery callback looks the most promising so far but I have no experience with it and am trying to make sure that's the path I should go down. Further complicating the issue is the fact that I do not have access to sharepoint builder so I seem to be stuck with the ECMA code available through the text editor.</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
  
  ExecuteOrDelayUntilScriptLoaded(showTable, "sp.js");
  var  tableField = "&lt;table&gt;&lt;tr&gt;&lt;th&gt;Site&lt;/th&gt;&lt;th&gt;Ticket&lt;/th&gt;&lt;th&gt;Last Update&lt;/th&gt;&lt;th&gt;Last Update Time&lt;/th&gt;&lt;th&gt;Controls&lt;/th&gt;&lt;/tr&gt;";
  
  function showTable()
  {
   var context = new SP.ClientContext.get_current();
   var web = context.get_web();
   var list = web.get_lists().getByTitle('BCTickets');
  
   var query = new SP.CamlQuery();
  
   query.set_viewXml('&lt;View&gt;&lt;Query&gt;&lt;Where&gt;&lt;Eq&gt;&lt;FieldRef Name="Active" /&gt;&lt;Value Type="Integer"&gt;1&lt;/Value&gt;&lt;/Eq&gt;&lt;/Where&gt;&lt;/Query&gt;&lt;ViewFields&gt;&lt;FieldRef 
  
  Name="TicketNumber" /&gt;&lt;FieldRef Name="Site" /&gt;&lt;FieldRef Name="Site" /&gt;&lt;/ViewFields&gt;&lt;/View&gt;');
  
   this.collListItem = list.getItems(query);
   context.load(collListItem);
  
   context.executeQueryAsync(Function.createDelegate(this, this.createTable), Function.createDelegate(this, this.failed));
  }
  
  function createTable()
  {
   var context = new SP.ClientContext.get_current();
   var web = context.get_web();
   var list = web.get_lists().getByTitle('Updates');
  
   var query = new SP.CamlQuery();
  
   var ListEnumerator = this.collListItem.getEnumerator();
  
   while(ListEnumerator.moveNext())
   {
    var currentItem = ListEnumerator.get_current();
  
    tableField += '&lt;TR&gt;&lt;TD&gt;' + currentItem.get_item('Site') + '&lt;/TD&gt;&lt;TD&gt;' + currentItem.get_item('TicketNumber') + '&lt;/TD&gt;&lt;TD&gt;';
  
    query.set_viewXml('&lt;View&gt;&lt;Query&gt;&lt;Where&gt;&lt;Eq&gt;&lt;FieldRef Name="BCTicketsID" /&gt;&lt;Value Type="Integer"&gt;' + currentItem.get_item('ID') + 
  
  '&lt;/Value&gt;&lt;/Eq&gt;&lt;/Where&gt;&lt;/Query&gt;&lt;ViewFields&gt;&lt;FieldRef Name="UpdateEntry" /&gt;&lt;FieldRef Name="UpdateDate" /&gt;&lt;/ViewFields&gt;&lt;/View&gt;');
  
    this.updateList = list.getItems(query);
  
    context.load(updateList);
  
    context.executeQueryAsync(Function.createDelegate(this, this.updateTable), Function.createDelegate(this, this.failed));
  
    tableField += '&lt;/TD&gt;&lt;TD&gt;&lt;/TD&gt;&lt;/TR&gt;';
  
   }
  
   tableField += "&lt;/TABLE&gt;";
  
   document.getElementById("innerTable").innerHTML = tableField;
  }
  
  function updateTable()
  {
   var ListEnumerator = this.updateList.getEnumerator();
  
   tableField += "&lt;TABLE&gt;";
  
   while(ListEnumerator.moveNext())
   {
    var currentItem = ListEnumerator.get_current();
  
    tableField += "&lt;TR&gt;&lt;TD&gt;" + currentItem.get_item('UpdateEntry') + "&lt;/TR&gt;&lt;/TD&gt;";
   }
  
   tableField += "&lt;/TABLE&gt;";
  }
  
  function failed(sender, args)
  {
   alert("failed. Message:" + args.get_message());
  }&lt;/script&gt;
  
  &lt;DIV ID="innerTable"&gt;&lt;/DIV&gt;
  </code></pre>