title: Build CAML dynamically with Javascript
body: <p>I'm looking for a function or code snippet or some starting help in creating a CAML builder in JavaScript.</p>
  
  <p>Building nested ORs shouldn't be a problem, e.g. (pid == 101 || pid == 102 || pid == 105)
  It's just a case of wraping groups of two in  tags. Similar for AND.</p>
  
  <p>Where I'm struggiling is combining ORs and ANDs in the same query.  Generally we will have a series dropdown boxes or checkboxes the user selects from. They can choose the not select certain fields which is why I need to build the CAML dynmically rather than have it ready to go and just inject the values into the appropriate places.</p>
  
  <p>EDIT:
  Here's what I have so far
  <a href="http://jsfiddle.net/QSPj2/4/" rel="nofollow">http://jsfiddle.net/QSPj2/4/</a></p>
  
  <p>So far it only builds either a series of ORs or a series of ANDs. You pass in an array of objects. Each object has field, comparison, type and value. The CAML builder will take this array and generate properly nested ANDs or ORs.  Use Dreamweaver formatting tool or similar on the output CAML to make it easier to read.</p>
  
  <p>It gets going to get a lot more tricky trying to combine ANDs with ORs.</p>