Count the number of items returned from a query ?

Jul 11, 2014 at 11:48 AM
I have a SharePoint list called Business Days, within this list a column called 'Date' is populated with Bank Holiday Dates, i.e. 25/12/2014, 26/12/2014, etc.

Within another list I am capturing a start date and an end date and im trying to get the number of bank holidays that fall between these dates, my code currently finds the values of the start date and end date fields and sets these into variables called startdate and enddate respectively.

My code looks like this:
$().SPServices(
    {  
        operation: "GetListItems",  
        async: false,  
        debug: true,  
        listName: "Business Days",
        CAMLQuery: "<Where><And><Geq><FieldRef Name='Date'></FieldRef><Value Type='DateTime'>" + sd + "</Value></Geq><Leq><FieldRef Name='Date'></FieldRef><Value Type='DateTime'> + ed + 

</Value></Leq></And></Where>",  
        CAMLViewFields: "<ViewFields></ViewFields>",  

        completefunc: function (xData, Status) 
        {  

            var datecount = $(xData.responseXML).SPFilterNode('z:row').length(function ()
            {
                $(this).attr('ows_Date');
            });
            alert(datecount);
        }

    })
Its running as a presave function returning false so it doesn't actually save at the moment, what im wanting is my variable 'datecount' to hold the number of bank holidays between the start date and end date so that if I pick a start date of 20/12/2014 and an end date of 30/12/2014, the variable datecount should hold the value of 2 (i.e. 1 for 25/12/2014 + 1 for 26/12/2014).

Its currently not working as expected, any help would be appreciated.

Thanks
Coordinator
Jul 11, 2014 at 12:37 PM
GetListItems also returns the count of items. The XML looks something like this:
<GetListItemsResult>
  <listitems xmlns:s="uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882" xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882" xmlns:rs="urn:schemas-microsoft-com:rowset" xmlns:z="#RowsetSchema">
    <rs:data ItemCount="9">
      <z:row>...</z:row>
      ...
So the rs:data element contains the value you want.
var datecount = $(xData.responseXML).SPFilterNode("rs:data").attr("ItemCount");
M.
Jul 11, 2014 at 1:05 PM
Thanks for the reply, I have tried the following:
    var sd = getUsableDate(startdate).toDateString();
    var ed = getUsableDate(enddate).toDateString();

        var query = "<Query>" + "<Where>" + "<And>" + "<Geq>" + "<FieldRef Name='Date' />" + "<Value Type='DateTime'>" + sd + "</Value>" + "</Geq>" + "<Leq>" + "<FieldRef Name='Date' />" + "<Value Type='DateTime'>" + ed + "</Value>" + "</Leq>" + "</And>" + "</Where>" + "</Query>";

 $().SPServices(
    {  
        operation: "GetListItems",  
        async: false,  
        debug: true,  
        listName: "Business Days",
        CAMLQuery: query,
        CAMLViewFields: "<ViewFields></ViewFields>",  

        completefunc: function (xData, Status) 
        {  
            var datecount = $(xData.responseXML).SPFilterNode("rs:data").attr("ItemCount");
            alert(datecount);
        }
    })
Its not working, if I try and place the alert(datecount); anywhere else (outside the } or even outside the following }) ) then the code doesn't execute.

Any clue what ive done wrong?
Jul 11, 2014 at 1:33 PM
Hi, never mind, its working now I fixed my CAML query.

thanks for your help.
Coordinator
Jul 11, 2014 at 1:33 PM
You should look at the returned XML to see what errors might be there, plus look at the Net traffic in Firebug or Fiddler.

Dates need to be in ISO format. There's a function in SPServices to convert JavaScript dates:
https://spservices.codeplex.com/wikipage?title=$().SPServices.SPConvertDateToISO

M.