Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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!");
share|improve this question

6 Answers 6

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 ;)

share|improve this answer

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.

share|improve this answer

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

share|improve this answer

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! :)

share|improve this answer
I was typing as fast as I could, Jimmy! ;) –  Jim Bob Howard Jan 26 '11 at 19:13
You type faster than my mind can think, Jim ;) –  James Love Jan 26 '11 at 20:19
Shame on you Jim Bob for not remembering we did this last week! –  Eric Alexander Jan 26 '11 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.

share|improve this answer

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

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.