I have been working with GetListItems, and found it to be very reliable, and easy to implement. One thing I'm trying to figure out is how to handle when there are no items in the list matching the query. It seems like when there are no matches, the alert(xData.responseText); is never fired. I have tried using ItemCount, but it seems to be too far into the function to work. Any guidance would be great

    operation: "GetListItems",
    async: false,
    listName: "Personnel",
    CAMLViewFields: "<ViewFields>" + 
                    "<FieldRef Name='EmployeeID' />" +
                    "<FieldRef Name='FirstName' />" +
                    "<FieldRef Name='LastName' />" +
                    "<FieldRef Name='Agency' />" +
                    "<FieldRef Name='FullName0' />" +
                    "<FieldRef Name='PayRate' />" +
    CAMLQuery: "<Query><Where><Eq><FieldRef Name='EmployeeID'/><Value Type='Text'>" + empID + "</Value></Eq></Where><OrderBy><FieldRef Name='Created' Ascending='False' /></OrderBy></Query>",
    CAMLRowLimit: 1,
    completefunc: function (xData, Status) {
        var validID = $(this).attr("ows_ID");
        var empBadgeID = $(this).attr("ows_EmployeeID");
        var empFirst = $(this).attr("ows_FirstName");
        var empLast = $(this).attr("ows_LastName");
        var empAgency = $(this).attr("ows_Agency");
        var empFullName = $(this).attr("ows_FullName0");
        var empPayRate = $(this).attr("ows_PayRate");
        //$("#txtFullName").val = empFullName();
        //$("#lblFullName").text = $(this).attr("ows_FullName");
        //var HTML =  "<li>" + "Badge ID: " + $(this).attr("ows_EmployeeID") + "</li>" + "<li>" + "First Name: " +  $(this).attr("ows_FirstName") + "</li>" + "<li>" + "Last Name: " +  $(this).attr("ows_LastName") + "</li>" + "<li>" + "Agency: " + $(this).attr("ows_Agency") + "</li>";



        if(Status == "success")
            var itemCount = $(xData.responseXML).find("[nodeName=rs:data]").attr("ItemCount");
            if(itemCount == '1')
                alert("found a valid record")
                alert("nothing to see here");
            alert("something we wrong!");

7 Answers 7


You could solve it with a working crossbrowser solution by replacing your string from:



$(xData.responseXML).find("z\\:row, row").each(function()

and then it will work in all browsers ;)


To see if an item exists in the list, you can look at the $(xData.responseXML).find("[nodeName=rs:data]").attr("ItemCount"); output of the getlistitems operation. We sort of hashed out something similar on stump the panel.


Be sure to put quotes around z:row or you will have issues with later versions of jQuery (1.5+, as I remember):


I've not used SPServices yet (Sorry Marc!) but from my limited knowledge of jQuery, I'd say it never gets fired when there are 0 rows because there are no z:row elements in the result set.

I'd set $(xData.responseXML).find("[nodeName=z:row]") to a variable then check it's length before continuing. A length of 0 indicates no rows (obv)

EDIT: seconds too slow! :)

  • I was typing as fast as I could, Jimmy! ;) Jan 26, 2011 at 19:13
  • You type faster than my mind can think, Jim ;)
    – James Love
    Jan 26, 2011 at 20:19
  • 1
    Shame on you Jim Bob for not remembering we did this last week! Jan 26, 2011 at 20:28

Put your alert(xData.responseXML.xml); as the first line inside the completefunc, so you can see what XML is coming back. If there's no nodeName=z:row, your current setup won't get to that alert.

To detemine if it's empty, you could set $(xData.responseXML).find("[nodeName=z:row]") to a variable. If its length is 0, nothing was returned.


Thank you all for your quick responses, it helped guide me to the solution. I added a line of code that checks for the item count(as suggested) to equal 1, and proceed with the function. If the count equal to zero, skip, and display no record found

var itemCount = $(xData.responseXML).find("[nodeName=rs:data]").attr("ItemCount");
    alert("itemCount :" + "" + itemCount);
if(itemCount == 1){

Thanks again, Jason


This question is a bit old but for the sake of posterity (and updated browsers as of Sep 2015) following code fragment worked for me in IE 9 and Chrome 44

            var itemCount = Number($(xData.responseXML).find("rs\\:data, data").attr("ItemCount"));

        if ((Status == "success") && (itemCount == 1)) {
            $(xData.responseXML).SPFilterNode("z:row").each(function() {

The "rs\:data" syntax didn't work in Chrome but worked in IE. The updated syntax "rs\:data,data" works in both browsers. The SPFilterNode function works in both browsers.

However, the correct answer above addresses z:row element which will not exist if there are no results to the query. The itemcount solution didn't use the correct syntax. Hence the need for this updated answer.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.