In Part 1 of this post I outlined a use case for using list items counter. In this post I will describe how you can create such a web part.

Prerequisites:

Step by step guide

  1. Open the page you want to customize with SharePoint Designer and insert a SharePoint Data View Control. (Click here to learn more.)
  2. If you want to list contents of the contacts list it will look like this:

  3. Right click on grid and select “Convert to XSLT Data View” option
  4. The first thing you will need to remove is table heading. From SPD open Source view of your page and look for HTML lines similar to ones below. Actual field names may vary for your list:
    <tr valign=”top”>

    <xsl:if test=”$dvt_1_automode = ‘1’” ddwrt:cf_ignore=”1″>

    <th class=”ms-vh width=”1%” nowrap=”nowrap”></th>

    </xsl:if><th class=”ms-vh nowrap=”nowrap”>Last Name</th>

    <th class=”ms-vh nowrap=”nowrap”>Modified By</th>

    <th class=”ms-vh nowrap=”nowrap”>Modified</th>

    </tr>

    You can comment or delete these lines from source code.

  5. Now you need to remove the actual values that are being displayed. The simplest way to do that is to look for xsl:for-each block. Replace the entire block with this simple code:
    <tr><td class=”ms-vb“>Total: <xsl:value-of select=”count($Rows)” /></td></tr>
  6. The above code will count all the nodes of your XML, and display only the count number accompanied with label “Total: “. Here is the preview of the result.

  7. For a web part that counts all the items in a list you do not need paging. Use Common Tasks to set paging to display all items.

The final result shown at figure below:

The technique described in this article can be used in various ways, and I will try to present some of these in a future article.

4 Comments

  1. Albert Buswell Says

    When I right click on the data view (any part, the grid, the header, etc.) I don’t see an option to convert to an xslt data view. I’m using Sharepoint Designer 2007 SP1.

  2. Toni Frankola Says

    @Albert: It looks like this: http://2.bp.blogspot.com/_ZLr3wvXiUTE/RkQd-XgGMdI/AAAAAAAAAhI/SOWTxLqJ6_g/s1600-h/convert-to-xslt.jpg

    You will have to position your mouse on the right spot to see it.

  3. Kevin Byrne Langlois Says

    Would it be possible to do the same with a library ? I tried with a form library with no success!

  4. Toni Frankola Says

    @Kevin: Sure, can you tell me what did you manage to do so far… and we can start from there…

Leave a Reply