Get an item count for a SharePoint list

Dec 10, 2012 at 11:38 PM

My end result is to perform a percentage complete calculation using the total items in a "Complete" status based on the total number of items in a list. I found information on an 'SPList.ItemCount' code; however, I am unsure how to use this and/or incorporate into the existing SPServices I currently use. Any ideas on how to obtain a total item count in a list?

I tried the 'GetListItems' code and was unsuccessful getting am item count.

Dec 11, 2012 at 3:55 AM

The total number of results that come back is within the <rs:data> node.  The attribute you want to get at is `ItemCount`.  So you'll want to pull all of the items from the list, then count the number of `Complete` items.  I'm doing this all from memory, so you may want to look at the response to see if this is all correct.  Here's an example (totally not tested either...):

 

completefunc: function( xData, Status ) {
	var $xData = $(xData.responseXML),
		itemTotal = 0,
		totalComplete = 0,
		percentage = 0
		
	; 
	
	itemTotal = $xData.SPFilterNode("rs:data").attr("ItemCount") * 1;
	
	$xData.SPFilternode("z:row").each(function(i, el) {
		var $node = $(this)
		
		;
		
		if ( $node.attr("ColumnForComplete") === "Complete" ) {
			totalComplete++;
		}
	}
	
	if ( itemTotal !== 0 ) {
		percentage = totalComplete / itemTotal;
	}
	
}

 

 

Cheers,
Matthew 

Dec 11, 2012 at 3:01 PM

Thank you Matthew.

Please forgive my next question; where do I place this code within my page code?

Dec 11, 2012 at 3:29 PM

You are going to want to look over the documentation to get up to speed.

http://spservices.codeplex.com/documentation#general-instructions

Post back if you get stuck.

Cheers,
Matthew 

Dec 11, 2012 at 7:07 PM

Matthew,

I am missing something somewhere. I followed the steps in the general instructions, which works on my other code, but I am not getting any results. I dont see anywhere in the sample code where to reference the site name, and tried to use alert() to see if the value can be seen in a pop-up to check the counting. None of which is working for me. Im at a loss as to what to do and look at next.

Dec 11, 2012 at 7:47 PM

Go ahead and post your code, so we have a place to start. Then we can iron out the details.

Cheers,
Matthew 

Dec 11, 2012 at 8:09 PM

here is the code Im using, which essentially is a copy/paste of the code provided:

 



<script language="javascript" type="text/javascript">

    $(document).ready(function() {

    completefunc: function( xData, Status ) {
 var $xData = $(xData.responseXML),
  itemTotal = 0,
  totalComplete = 0,
  percentage = 0
  
 ;
 
 itemTotal = $xData.SPFilterNode("rs:data").attr("ItemCount") * 1;
 
 $xData.SPFilternode("z:row").each(function(i, el) {
  var $node = $(this)
  
  ;
  
  if ( $node.attr("PM_Value") === "Complete" ) {      //This is the display name for the column. The field reference name is "PM%5FValue"
   totalComplete++;
  }
 }
 
 if ( itemTotal !== 0 ) {
  percentage = totalComplete / itemTotal;
 }
        alert("Total items: " + itemTotal);
        alert("Percentage complete: " + percentage);
 
     }

 

    });

</script>

Coordinator
Dec 12, 2012 at 3:32 AM

William:

The code that Matt gave you is only to part of what you need to get at the ItemCount from a call to GetListItems. You've put it into a $(document).ready() block but you aren't calling GetListItems.

M.