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.
- Microsoft Office SharePoint Designer 2007
- One contacts list (or any other list) with some sample data
- Basic knowledge of HTML and XSLT
Step by step guide
- Open the page you want to customize with SharePoint Designer and insert a SharePoint Data View Control. (Click here to learn more.)
- If you want to list contents of the contacts list it will look like this:
- Right click on grid and select “Convert to XSLT Data View” option
- 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:
<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>
You can comment or delete these lines from source code.
- 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>
- 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 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.
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.