1

I have a CAML query. Users want to select more than two country from the check box at one time and search. How can I do array in CAML ?

<input type="checkbox"  name='country' value="Global/Other">Global/Other</input><br>
<input type="checkbox" name='country'value="Afghanistan" checked="checked">Afghanistan</input><br>
<input type="checkbox" name='country'value="Albania">Albania</input><br>
<input type="checkbox" name='country'value="Algeria">Algeria</input><br>
<input type="checkbox" name='country'value="Andorra">Andorra</input><br>
<input type="checkbox"  name='country' value="Angola">Angola</input><br>
  • What is the other technology you're using in combination with this? JavaScript? InfoPath? – Graham Aug 14 '17 at 15:54
  • I'm using JavaScript – Sara Aug 14 '17 at 16:13
2

I've never gotten an array to work in CAML, but you can use code to build up your CAML. You just need to follow the pattern that between and ORs or ANDs, you can only have 2 statements.

<OR><OR><OR>
<Eq><FieldName Ref="XXX"><Value="Text">YOURVALUE<Value></Eq>
<Eq><FieldName Ref="XXX"><Value="Text">YOURVALUE<Value></Eq>
</OR>
<Eq><FieldName Ref="XXX"><Value="Text">YOURVALUE<Value></Eq>
</OR>
<Eq><FieldName Ref="XXX"><Value="Text">YOURVALUE<Value></Eq>
</OR>

So you'll need to take in account any other values you need, but you can build up part of this similar to this:

    $('.fmgSelectUser').each(function() {
             if($(this).is(':checked')) {
                 if(count ==0){
                 CAMLselect += "<Eq><FieldRef Name='" + fieldName + "' LookupId='TRUE' /><Value Type='Choice'>" + $(this).val()+ "</Value></Eq>";
                 } else if(count == 1) {
    CAMLselect += "<OR> + CAMLSelect + "<Eq><FieldRef Name='" + fieldName + "' LookupId='TRUE' /><Value Type='Choice'>" + $(this).val()+ "</Value></Eq>" + "</OR>"; 

} else {
                 CAMLselect = "<Or>" + CAMLselect + "<Eq><FieldRef Name='" + fieldName + "' LookupId='TRUE' /><Value Type='Choice'>" + $(this).val()+ "</Value></Eq></Or>";
                 }
                 count++;
             }

         });

The first 2 statements will go together in a single OR, and then the rest of them wrap what you've got in a new

2

I believe you can make use of IN operator.

Get all the checked check boxes using jQuery and store it in a variable.

var checkedValues = '';
$('input[name="country"]:checked').each(function() {
   console.log(this.value);
   checkedValues += "<Value Type='text'>" + $(this).val() + "</Value>";
});

Once you have all the values, you can use the IN operator for retrieving the values. You can store something like below for your caml query and execute further.

'<Where>' +
    '<In>' +
        '<FieldRef Name="Country"/>' +
        '<Values>' +
             checkedValues +  
        '</Values>' +
    '</In>' +
'</Where>'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.