This project has moved and is read-only. For the latest updates, please go here.

Dynamic CAMLQuery

Sep 17, 2012 at 7:40 PM

I'm working on a function to fetch data from a list called Comments. This comments list has a lookup to my pages library.  I'm trying to fetch all comments for a particular page.  This is what I'm doing:

function GetComments(theItem){
   webURL: thisSite,
      operation: "GetListItems",
      async: false,
      listName: "Comments",
      CAMLViewFields:  "<ViewFields><FieldRef Name='Author' /><FieldRef Name='_Comments' /></ViewFields>",
      CAMLQuery: "<Query><Where><Eq><FieldRef Name='News_x0020_River_x0020_Article' LookupId='True' /><Value Type='Lookup'>" + theItem + "</Value></Eq></Where>" +
         "<OrderBy><FieldRef Name='Created' Ascending='FALSE' /></OrderBy></Query>",
      completefunc: function (xData, Status) {
       var ic = 0;
       ic = $(xData.responseXML).SPFilterNode("rs:data").attr("ItemCount");
       if (ic > 0){
        $("#news-article-comment-header").text("Comments (" + ic + ")");
        $(xData.responseXML).SPFilterNode("z:row").each(function() {
         //Do Stuff

       else {
        $("#news-article-comment-header").text("No Comments");

This returns no items.  When I hard code the value 2, it returns 5 comments.  Console logging the value resolves to the value 2 properly. Not sure what is going on here. Anything look weird?

Sep 17, 2012 at 8:22 PM

Try building the CAMLQuery in a variable each way and compare the resulting strings. It's possible that they are not the same, though it seems they should be.

Sep 17, 2012 at 8:56 PM

Good suggestion, $.trim() ftw :-(

Sep 17, 2012 at 10:36 PM



Feb 26, 2013 at 5:52 AM
Edited Feb 26, 2013 at 3:12 PM
I have 3 dropdowns. the first 2 are parameters for dropdown # 3.

relationshipList: "Servicios del salón",
relationshipListParentColumn: "saloninterno",
relationshipListChildColumn: "Title",
parentColumn: "Salón Interno",
childColumn: "Salón Servicios",
CAMLQuery: "<Leq><FieldRef Name='LimiteAsistentes' /><Value Type='Number'>"+ CapacidadSalon +"</Value></Leq>",
debug: true

Please can you explain me how I can do that?... I can't put dynamic CAMLQuery in . I set a variable CapacidadSalon initial = 1; and always search with 1. but I change the dropdown #2 no update the dropdown # 3 because variable CapacidadSalon always is 1.

How I can do that?

Thank you for your help!
Feb 26, 2013 at 8:37 PM

The CAMLQuery is evaluated only upon instantiation of the function, not on each change.