1

I have to send more than one ID to CAML query. But got struck with the or condition. How can we implement or condition for unknown number of ID's to be passed to the query. I have done in for two ID's, now I need to do it for unknown number of ID's to be passed.

$('#tblCustomListData tbody').empty();
var array = $('#userid').val().split(",");

var camlQuery ="<Or>";
$.each(array,function(i){
   array[i] = array[i].trim();
   camlQuery = camlQuery + "<Eq><FieldRef Name='ID' /><Value Type='Text'>" + array[i] + "</Value></Eq>";


});
camlQuery2 = camlQuery + "</Or>";
camlQuery2 = "<View Scope='RecursiveAll'><Query><Where>"+ camlQuery2 +"</Where></Query></View>";

var context = new SP.ClientContext.get_current();
var web = context.get_web();
var list = web.get_lists().getByTitle('DocumentList');
var fileName = document.getElementsByName('userid')[0].value;

var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml(camlQuery2);
allItems = list.getItems(camlQuery);
context.load(allItems, 'Include(Title,DocumentTitle,Description0,DocID,OHRID,Modified,FileRef)');

How do we implement a query so that it work for any number of ID's.

4

I think the easiest way to build a query for an unknown number of IDs is to use the <In> operator. It's kind of like a multiple Or statement without having to nest all the Ors.

You could do something like:

var query = '<Where><In><FieldRef Name="ID" /><Values>';

array.forEach(function(id) {
    query += '<Value Type="Number">' + id + '</Value>';
});

query += '</Values></In></Where>';

Apparently the In operator has a limit of 500 items, so see this other question about how to deal with a situation involving more than 500 items in an In query.

2

Here's how I build up a CAML statement with an unknown number of items. This is looping through a bunch of checkboxes to build an OR complex, but the basic structure is: Build the outside of the query with any required ANDs. Build the inside of the ORs - the first is just the Eq statement, the rest keep wrapping what you've got in s Maybe you can translate some of that logic to what you need.

var query = "<Query><OrderBy><FieldRef Name='Launch_x0020_Date'/></OrderBy>";
    query += "<Where>"; 
    query += "<And><And>";
    query += "<Geq><FieldRef Name='Launch_x0020_Date'/><Value Type='DateTime' IncludeTimeValue='FALSE'>" + dateF + "</Value></Geq>";
    query += "<Neq><FieldRef Name='Status'/><Value Type='Choice' >00-Not Started</Value></Neq>";
    query += "</And>";
    query += buildCAMLSelect('Coder');
    query += "</And>";
    query += "</Where></Query>";
function buildCAMLSelect(fieldName) {
     var CAMLselect = '';
     var count = 0;
     $('.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 {
             CAMLselect = "<Or>" + CAMLselect + "<Eq><FieldRef Name='" + fieldName + "' LookupId='TRUE' /><Value Type='Choice'>" + $(this).val()+ "</Value></Eq></Or>";
             }
             count++;
         }

     });
     return CAMLselect;
 }

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.