Count list items where field X equals Y, then count them and return a variable

Nov 13, 2013 at 1:50 PM

I've been trying to use the GetListItems operation to return data from a list, then using the count of the data matching the CAMLquery to update a variable.

The purpose of this is to determine available seats for various sessions/courses.

Here's what I have tried so far:
var seats = 5;

$(document).ready(function() {
    var usedSeats = 0;
        operation: "GetListItems",
        async: false,
        webURL: "/kursinnhold/",
        listName: "Kurs",
        CAMLQuery: "<Eq><FieldRef Name = "Title" /><Value Type="Text">Personalstyring</Value></Eq>",
        CAMLViewFields: "<ViewFields><FieldRef Name='Title' /></ViewFields>,
        completefunc: function (xData, Status) {
             $(xData.responseXML).SPFilterNode("z:row").each(function() {
                usedSeats += 1;
                var availableSeats = seats - usedSeats;
Basically, I want to parse through the rows of results that are returned when I query the list for title=somevalue, then for each row add 1 to a counter, then use a preexisting variable (seats) and substact the counter from it to return the available seats.

Can anyone point me in the right direction?
Nov 13, 2013 at 2:27 PM
I'd rather use the attribute on the rs:data element that already tells you the count of items returned by the query.
Nov 13, 2013 at 8:00 PM
I'm currently doing something very similar. Here's a snip of how I do it:
//Get registered attendees
    operation: "GetListItems",
    async: false,
    listName: "Class Registration",
    CAMLViewFields: "<ViewFields><FieldRef Name='ID' /><FieldRef Name='Class' /><FieldRef Name='Title' /><FieldRef Name='FirstName' /></ViewFields>",
    CAMLQuery: "<Query><Where><Eq><FieldRef Name='Class'></FieldRef><Value Type='Text'>" + strClassNumber  + "</Value></Eq></Where><OrderBy><FieldRef Name='Title' /><FieldRef Name='FirstName' /></OrderBy></Query>",
    CAMLQueryOptions: "<QueryOptions><IncludeMandatoryColumns>FALSE</IncludeMandatoryColumns></QueryOptions>",
    completefunc: function (xData, Status) {
        //If the class is full, disable the request attendance button
        var intSlotsRemaining = intCapacity - $(xData.responseXML).SPFilterNode("rs:data").attr("ItemCount");   //intCapacity comes from another list
        if(intSlotsRemaining > 1){
            strCapacityMessage = "There are " + intSlotsRemaining + " slots remaining for this class.";
        else if(intSlotsRemaining == 1){
            strCapacityMessage = "There is " + intSlotsRemaining + " slot remaining for this class.";
            strCapacityMessage = "This class is currently full.  Please try another class or check this one later for vacancies."
            $("#ButtonRequestAttendance").attr("disabled", "disabled");
Nov 14, 2013 at 11:10 AM
Edited Nov 14, 2013 at 11:40 AM
Thanks Geoff,

The logic behind what you're doing is very similar to my task - thanks for sharing!

The issue I'm faced with prevents me from getting it right though - the responseXML is currently "null", and I cannot for the life of me figure out why that is.

Here's what I now am trying:
$( document ).ready(function() {
        operation: "GetListItems",
        async: false,
        webURL: "/kursinnhold/",
        listName: "{98E7E0B1-E8CE-4A79-8414-5C9C030D7969}",
        CAMLViewFields: "<ViewFields><FieldRef Name='Title' /><ViewFields><FieldRef Name='ID' /></ViewFields>",
        CAMLQuery: "<Query><Where><Or><IsNotNull><FieldRef Name='ID'></FieldRef></IsNotNull><IsNotNull><FieldRef Name='Title'></FieldRef></IsNotNull></Or></Where></Query>",
        CAMLQueryOptions: "<QueryOptions><IncludeMandatoryColumns>FALSE</IncludeMandatoryColumns></QueryOptions>",
        completefunc: function (xData, Status) {
                console.log("The ResponseXML is: " + xData.responseXML);
                var usedSeats = plasser - $(xData.responseXML).SPFilterNode("rs:data").attr("ItemCount"); //plasser comes from another variable
I am testing this against a list where I have got the UpdateListItems operations working, so the webURL and listName ought to eb correct and access should be ok.

The response to the console is: The ResponseXML is: null

Where should I begin troubleshooting? Where could my code be going wrong?

Thanks for the responses so far!

edit: When I use console.log(xData.responseText); I get the result I expected - which led me to google, which again made me set async to true.

For some reason, this made me get a result.

I'll post back with an update once I get it working.
Nov 14, 2013 at 11:45 AM
First of all look at the request and response header Accept and Content-type. Then at the browser mode.
Nov 14, 2013 at 1:27 PM
Thanks ACrush, but the issue seems to stem from setting async to false - at least that appears to be the case for both Chrome and IE 10 where I've tested it.

In any case, the end product, which works, is as follows:
$(document).ready(function() {
        operation: "GetListItems",
        async: true,
        webURL: "/kursinnhold/",
        listName: "{98E7E0B1-E8CE-4A79-8414-5C9C030D7969}",
        CAMLViewFields: "<ViewFields><FieldRef Name='Title' /></ViewFields>",
        CAMLQuery: "<Query><Where><Eq><FieldRef Name='Title'></FieldRef><Value Type='Text'>" + Tittel + "</Value></Eq></Where></Query>", //only return hits that match the currently selected search hit
        CAMLQueryOptions: "<QueryOptions><IncludeMandatoryColumns>FALSE</IncludeMandatoryColumns></QueryOptions>",
        completefunc: function (xData, Status) {
                var usedSeats = $(xData.responseXML).SPFilterNode("rs:data").attr("ItemCount"); //plasser comes from another variable and contains the total available seats for this particular event
                var availableSeats = plasser - usedSeats;
                document.getElementById('ledigeplasser').innerHTML = availableSeats; //updates a field in a table
                if (availableSeats < 1) {
                    document.getElementById('pameldingknapp').innerHTML = "<span style='color:red'><strong>This event is full</strong></span>"; //replaces the registration button
A big thank you to the community for helping me, I only picked up the SPServices library a few days ago, and so far I think it's a fantastic tool, and one I'll definitely will keep using in the future as well. So for now, here's a small contribution :)

Nov 14, 2013 at 2:03 PM
Thank you for the contribution, Kristian! Every little bit helps me keep this prioritized (though I don't do it for the money).

Also be sure to share any cool stuff you come up with using SPServices in case it may help others or be something I can add to the library.

Thanks again,
Nov 14, 2013 at 4:20 PM
Re: "but the issue seems to stem from setting async to false - at least that appears to be the case for both Chrome and IE 10 where I've tested it. "

This may be something to look at to ensure it is not a bug in SPServices.

What versions of the following are you using:

- jQuery
- SPServices
- Sharepoint version

I'll run a few tests in chrome to see if I can recreate.



Paul T

Nov 18, 2013 at 7:22 AM
Hi Paul,

These are the versions I'm using:

jQuery: jquery-1.10.2.min.js
SPServices: jquery.SPServices-2013.01.min.js
SharePoint 2013: 15.0.4517.1005

The context in which I am running SPServices is from within a "product catalog" site, where jQuery/SPServices is called from the control template, and then the script is run within the item display template.

If need be, I can send you the files, perhaps by email?
Or if it's easier, as this is a lab/test environment and holds nothing confidential I could simply grant you access to the site.
Nov 18, 2013 at 2:10 PM
Gaining temporary access to then env would be great. It would avoid having to spend time doubt the setup.

Can you drop me an email: ptavares [at]

Thinking a little more about this: it could the browser. I the version of IE above. What version of Chrome are you using?

Paul T.

-- Sent from Mobile