Returning ItemCount

Oct 11, 2009 at 8:14 PM

Marc

Could you please help me on how I would obtain the ItemCount value using the GetListItem webservice? I was thinking along the lines of; $(xData.responseXML).find('rs\\:data').attr('ItemCount'); but no luck.

Thanks

Marcus

Coordinator
Oct 12, 2009 at 3:01 AM
Edited Oct 12, 2009 at 3:02 AM

Marcus:

You're doing the right thing, but you may be getting caught by the default asynch behavior. The code below works because I explicitly set async to false.  This forces the call to GetListItems to occur "while you wait".  By default the behavior is asynchronous, so when you try to grab the value for ItemCount, it isn't there yet!

This is the type of tip that I need to build into the documentation, so thanks for asking the question.

M.

function callGetListItems() {
 var itemCount;
 $().SPServices({
  operation: "GetListItems",
  listName: testList,
  async: false,
  completefunc: function (xData, Status) {
    $(xData.responseXML).find("rs\\:data").each(function() {
     itemCount = $(this).attr("ItemCount");
    });
  }
 });
 alert(itemCount);
}

Oct 12, 2009 at 3:30 PM

Thanks for the quick response and following your example above worked. Can you recommend an more elegant syntax as far as obtaining both the itemCount and iterating through the collection? This is what I have but there should be a better way.

function callGetListItems() {
 var itemCount;
 $().SPServices({
  operation: "GetListItems",
  listName: testList,
  async: false,
  completefunc: function (xData, Status) {
    $(xData.responseXML).find("rs\\:data").each(function() {
     itemCount = $(this).attr("ItemCount");
    });        
    $(xData.responseXML).find("z\\:row").each(function(){  
      returnID  = $(this).attr('ows_ID');
      $("input[title=Course ID]").val(returnID);   
     });
    }
 });
 alert(itemCount);
}

 

Thanks again for everything

Marcus

Coordinator
Oct 12, 2009 at 4:00 PM

Glad it works for you! We could probably tighten up that code a little bit, but can you explain a little about what you are trying to accomplish with it?

M.

Jun 8, 2010 at 3:33 PM

I dont see why you would loop through to get the item count

your response comes back as:

 

<rs:data ItemCount="2">

 <z:row/>

<z:row/>

</rs:data>

 

I was able to get the item count straight away with this (shown in reference to node looping)

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

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

{

}

 

 

Aug 20, 2010 at 3:00 AM

Hi, I want to ask a question related to ItemCount. I used the GetListItem web service but I only want to get the ItemCount alone and not the whole data based on the caml query because my data contains hundreds of rows(It slows my performance a lot).

Is there anyway to achieve this ?Thank you.

Coordinator
Aug 20, 2010 at 10:12 AM
I don't think so. GetList will return the number of items in the list, but you can't do any filtering. You could limit the ViewFields to just the ID to make the returned XML as small as possible. The whole point of GetListItems is to return the items themselves. Hundreds of items ought not to be any more problem than several, based on my experience. Depending on what you are actually trying to accomplish, maybe the Web Services aren't the best answer. If you are just trying to get counts of items, consider using a DVWP. M.
Coordinator
Aug 20, 2010 at 3:00 PM

I see Marc already answered, and what he suggested is what I did the last time I needed to do this. Just limit ViewFields to just the ID so you're not pulling back a lot of data. The web service is going to do the full query anyway, so just limit what you're querying and you should be fine.