CAML Query in SPServices GetListItem function returning all values instead of filtered

May 20, 2015 at 7:13 PM
Hello everyone,

I've been working on a script that accesses a SharePoint list and tries to pull the values from one column of that list where the id value in another column matches the value I'm passing. Below is the code I've written. For some strange reason, the CAML Query is returning all values from the list instead of the 12 desired values I was looking for. I've tried adding/removing just about every combination of tags I can to the beginning and end of the CAML Query based on other discussions I've read....and now I'm desperate to find a solution. PLease please please help me find my flaw.
    log("Start");  //log is a function I created that outputs to a web part on my sharepoint page so that I use it to debug and test my code.  This is to confirm the code is running.
    log(vbcid);  //this is to confirm the ID value I'm passing to the CAML Query is valid
    var milestonesArray = [];  //this is an array I created to store the results of the function
var method = "GetListItems";
var list = "Business Case Milestones";
var fieldsToRead = "<ViewFields><FieldRef Name='Milestone' /></ViewFields>";
    var test = "<Query>";  //test I created while I was debugging the issue
    log(test);  //based on the code above, this log outputted an empty string to the web part
    alert(test);  //alert showed the full string value of <Query>

    var query = "<Query><Where><Eq><FieldRef Name='Title' /><Value Type='Text'>"+vbcid+"</Value></Eq></Where></Query>";
    //above line is the main query that I'm having issues with

log(query);  
    //when I debug the query, the log only outputs the value of vbcid to the web part
    alert(query);  //however the alert shows the full query string
$().SPServices({
        operation: method,
        async: false,
        listName: list,
        CAMLViewFields: fieldsToRead,
        CamlQuery: query,
        completefunc: function (xData, Status) {
          $(xData.responseXML).SPFilterNode("z:row").each(function() {
             var milestone = ($(this).attr("ows_Milestone"));
                         //storing each returned row value
             milestonesArray.push(milestone);  //adding the returned value to my array
             });
        }
});
log(milestonesArray.length);
/ log at the end shows the length of the array is 1286 which is full list rather than the 12 rows I was expecting. If I do a log of the values themselves, it shows that the row values were retrieved correctly, it just retrieved all of them rather than the 12 I wanted filtered. /
Coordinator
May 20, 2015 at 7:16 PM
Spelling counts!
CamlQuery: query,
should be
CAMLQuery: query,
M.
May 20, 2015 at 7:19 PM
I had been looking at this code for over two days (with about 5 hours of sleep mixed in sporadically). I think I want to kick myself. Thanks for the quick solution though!!!
Coordinator
May 20, 2015 at 7:23 PM
When I get into a hole like that, lots of times I just start from scratch, assuming that I have a typo that my eyes aren't seeing.

Glad it worked!

M.