Build CAML query from Array

Nov 11, 2013 at 2:01 PM
Edited Nov 11, 2013 at 2:02 PM
This is really cool tool but I've difficulties getting this implemented in JS. I did refer to camljs.js in my script and using the below code but nothing happens..

pcvalues contains an array of postal codes and I want to build CAML based on that, here is the code..(I'm using clientcontext to get the internal name and values of the column(textfield)).
function onQuerySucceeded() {
    var listInfo = '';
   for (var i = 0; i < this.listInfoArray.length; i++) {
        var oList = this.listInfoArray[i];
        var collField = oList.get_fields();
        var fieldEnumerator = collField.getEnumerator();

   while (fieldEnumerator.moveNext()) {
            var oField = fieldEnumerator.get_current();
            var regEx = new RegExp('LocalBusinessPostalcode', 'ig');
            if (regEx.test(oField.get_internalName())) {
                listInfo = oField.get_internalName();
               //Caml Start
                var postalcodes = [pcvalues];
                var postalcodesExpressions = [];
                for(var i = 0;i<postalcodes.length;i++){
                var postalcodesBuilder = CamlBuilder.Expression();
                var caml = new CamlBuilder().Where().All(
                postalcodesBuilder.Any.apply(postalcodesBuilder, postalcodesExpressions) 
                //Caml End 
nothing happens with alert too.. question is .ToString() is not there JS so wondering if that is the issue. Any help would be appreciated.
Nov 11, 2013 at 2:19 PM
Update: Now I see in the console that 'CamlBuilder' is undefined in IE console, i did triple check the reference its good to go.. FYI env is SP 2013.
Nov 13, 2013 at 12:31 PM
Edited Nov 13, 2013 at 12:31 PM

I'm using Caml Builder in SP2013 and no issues. Something is wrong with how you're adding the script to the page.
You can use IE Developer Tools to troubleshoot this:
  1. Ensure that the script reference is declared on the page by searching for "camljs.js" in the Script tab.
  2. Under the Network tab, enable the capturing and ensure that the camljs.js is indeed fetched successfully from the server.
P.S. Sorry for the late response.
Nov 14, 2013 at 4:20 PM
Not sure if this helps anyone but I was able to get an array to work with .All()
I am still somewhat of a novice with JavaScript so if you see something that looks odd/wrong let me know! :)
In this example I am using 4 columns in a situation where the query length in terms and columns can both vary.
The *_Query[i] statements are just the term values chosen by the user to be filtered.

var Column_A_Exp = [];
for (var i = 0; i < Column_A_Query.length; i++) {
var Column_B_Exp = [];
for (var i = 0; i < Column_B_Query.length; i++) {
var Column_C_Exp = [];
for (var i = 0; i < Column_C_Query.length; i++) {
var Column_D_Exp = [];
for (var i = 0; i < Column_D_Query.length; i++) {

var Column_A_CAMLBuilder = CamlBuilder.Expression();
var Column_B_CAMLBuilder = CamlBuilder.Expression();
var Column_C_CAMLBuilder = CamlBuilder.Expression();
var Column_D_CAMLBuilder = CamlBuilder.Expression();

var Column_A_Apply;
var Column_B_Apply;
var Column_C_Apply;
var Column_D_Apply;

var makeArray = new Array();

if (Column_A_Query.length != 0) {
    Column_A_Apply = Column_A_CAMLBuilder.Any.apply(Column_A_CAMLBuilder, Column_A_Exp);
else delete Column_A_Apply;
if (Column_B_Query.length != 0) {
    Column_B_Apply = Column_B_CAMLBuilder.Any.apply(Column_B_CAMLBuilder, Column_B_Exp);
else delete Column_B_Apply;
if (Column_C_Query.length != 0) {
    var Column_C_Apply = Column_C_CAMLBuilder.Any.apply(Column_C_CAMLBuilder, Column_C_Exp);
else delete Column_C_Apply;
if (Column_D_Query.length != 0) {
    var Column_D_Apply = Column_D_CAMLBuilder.Any.apply(Column_D_CAMLBuilder, Column_D_Exp);
else delete Column_D_Apply;

var caml = new CamlBuilder().Where().All.apply(CamlBuilder.Expression(),makeArray).ToString();
caml = "<Where>"+caml+"</Where>";
Feb 4, 2014 at 9:57 AM
If you are on sharepoint2007 you also need sp.core.js and sp.runtime.js. You can get it from some existing Sharepoint2013 site.
Sep 21, 2014 at 12:23 AM
Edited Oct 11, 2014 at 12:01 AM
Two updates:
  1. Starting from CamlJs 2.5, the library is no longer dependent on anything external. So you can use it in SP2007 or anywhere else without any limitations. Please don't forget that some CAML Query features supported by CamlJs were only added in SP2010 and are not available in SP2007.
  2. All and Any now support the array parameter, e.g. like this:
var arrayOfExpressions = [];
// ...
var caml = new CamlBuilder().Where().All(arrayOfExpressions);
The version that supports this syntax is available via Nuget:
Install-Package CamlJs
, or alternatively just download the latest camljs.js file.