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

 $().SPServices({
    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' />" +
                    "</ViewFields>",
    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) {
      $(xData.responseXML).find("[nodeName=z:row]").each(function() 
      {
        alert(xData.responseText);
        alert(xData.responseXML.xml);
        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");
        //alert(empFullName);
        //$("#txtFullName").val = empFullName();
        //$("#lblFullName").text = $(this).attr("ows_FullName");
        $("#txtFullName").val(empFullName);
        $("#txtLastName").val(empLast);
        $("#txtFirstName").val(empFirst);
        $("#txtAgency").val(empAgency);
        $("#txtPayRate").val(empPayRate);
        //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>";

        //$("#mydiv").append(HTML);

        //$('#mydiv').dialog('open');

        if(Status == "success")
        {
            var itemCount = $(xData.responseXML).find("[nodeName=rs:data]").attr("ItemCount");
            if(itemCount == '1')
            {
                alert("found a valid record")
            }
            else
            {
                alert("nothing to see here");
            }
        }
        else
        {
            alert("something we wrong!");
        }
      });
    }
  });
}
share|improve this question
add comment

6 Answers

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
add comment

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

$(xData.responseXML).find("[nodeName='z:row']").each(function() 
share|improve this answer
add comment

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

$(xData.responseXML).find("[nodeName=z:row]").each(function()

to:

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

and then it will work in all browsers ;)

share|improve this answer
add comment

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
1  
Shame on you Jim Bob for not remembering we did this last week! –  PirateEric Jan 26 '11 at 20:28
add comment

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
add comment

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
add comment

Your Answer

 
discard

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