GetListItems - No Items to return

Jan 26, 2011 at 5:52 PM

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

 

Thanks,

Jason

    $().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!");
		}
      });
    }
  });
}

Coordinator
Jan 26, 2011 at 6:14 PM

Jason:

If no items are returned, then there will be no z:row elements and your .find() won't return anything. If you look at the documentation on MSDN which shows the return value (http://msdn.microsoft.com/en-us/library/lists.lists.getlistitems.aspx), you'll see that you should get something like this:

<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" 
   xmlns="http://schemas.microsoft.com/sharepoint/soap/">
   <rs:data ItemCount="4">
      <z:row ows_Number_Field="6555.00000000000" 
         ows_Created="2003-06-18T03:41:09Z" 
         ows_ID="3" ows_owshiddenversion="3" />
      <z:row ows_Number_Field="78905456.0000000" 
         ows_Created="2003-06-18T17:15:58Z" 
         ows_ID="4" ows_owshiddenversion="2" />
         ...
   </rs:data>
</listitems>

If there are no items returned, ItemCount will be zero and there will be no z:row elements:

<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" 
   xmlns="http://schemas.microsoft.com/sharepoint/soap/">
   <rs:data ItemCount="0"></rs:data>
</listitems>

So test for the value in ItemCount. If there is one, then you probably don't have an error, but just no returned items.

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

M.

Coordinator
Jan 26, 2011 at 6:15 PM

Oops. Just noticed that you are already trying to grab ItemCount. However, you're doing it inside the .find() for z:rows. There won't be any!

M.

Jan 26, 2011 at 6:24 PM

How can I handle a situation where there are no z:rows? I have been digging through the documentation, but can't seem to find any reference to this.

 

Thanks,

Jason

Coordinator
Jan 26, 2011 at 6:26 PM

Just check the ItemCount in the rs:data element BEFORE you try to find the z:rows. If it is 0, then do [whatever you want to do], if GT 0, then proceed.

M.

Jan 27, 2011 at 2:12 AM

Marc,

     thank you for all of the help. I have successfully added the functionality that I was looking for with your guidance. 

 

Jason

 

completefunc: function (xData, Status) 
    {
        var itemCount = $(xData.responseXML).find("[nodeName=rs:data]").attr("ItemCount");
		alert("itemCount :" + "" + itemCount);
	if(itemCount == 1){
      $(xData.responseXML).find("[nodeName=z:row]").each(function() {
        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>";
		
		$("#mydiv1").append(HTML);
		
		//$('#mydiv').dialog('open');

		
      });
      }
      else
      {
      	alert("no record found!");
      }
    }

Coordinator
Jan 27, 2011 at 10:46 AM
Great, Jason. Glad you got it up and running! M.