SharePoint and Anchors

Recently a customer asked me to add anchors to a SharePoint list. They need to use the “list view > all” page but had 500 or more items in the list and wanted an anchor bar at the top of the page so they click on a link to take them directly to anywhere from A to Z. Time for some XSLT.

The first step was to copy the default view page with SPD and set the list view web part to be an XSLT webpart instead. Interesting this gave SPD huge problems… doing a split view resulted in a huge wait time to display the page. Anyway, once we’ve got our list view being rendered via XSLT we can make some changes. What we want to do looks a bit like this :

- Pull the next item from the list
- Does the character @Title starts with differ from the character the title starts with for the previous list item ?
- If so, time to use an anchor.

I’d like a list of anchors displayed at the top of my web part for me to click on, so let’s set that up first :

<!-- replace the class ID with the appropriate one for your page -->
   <div Class=”{$CLASSID}” align=”right”>
   <xsl:text>Jump to : </xsl:text>
      <xsl:for-each select=”$Rows”>

Initially this seems fairly simple, however XSLT is case sensitive whereas SharePoint isn’t. Let’s say we’ve got 4 list items : “Funky”, “fun”, “Frightening” and “flattened”. If we don’t take casing into account we’ll end up with 4 anchors all for the letter F. In order to get around this, we’ll translate all our Titles into lower case. So, let’s start building our variables…

      <xsl:variable name=”lcletters”>abcdefghijklmnopqrstuvwxyz</xsl:variable>
      <xsl:variable name=”ucletters”>ABCDEFGHIJKLMNOPQRSTUVWXYZ</xsl:variable>
      <xsl:variable name=”previousstartchar” select=”substring(preceding-sibling::*[1]/@Title,1,1)” />
      <xsl:variable name=”casedpreviousstartchar” select=”translate($previousstartchar,$ucletters,$lcletters)” />
      <xsl:variable name=”currentstartchar” select=”substring(@Title,1,1)” />
      <xsl:variable name=”casedcurrentstartchar” select=”translate($currentstartchar,$ucletters,$lcletters)” />

At this point we need to get the first character of the previous list item’s title into a variable and get that into lower case too.

      <xsl:variable name=”previousstartchar” select=”substring(preceding-sibling::*[1]/@Title,1,1)” />
      <xsl:variable name=”currentstartchar” select=”substring(@Title,1,1)” />

All our variables are loaded up, so it’s time to do our comparison and parse our way through the list.

      <xsl:if test=”$casedpreviousstartchar != $casedcurrentstartchar”>
         <A HREF=”#{@Title}”><xsl:value-of select=”$casedcurrentstartchar” /></A><xsl:text> </xsl:text>
      </xsl:if>
   </xsl:for-each>
</div>

Pretty simple really. When it comes time to add the anchors to the body of our list, we just need to do the same XSL:IF for each item and add an anchor tag if necessary. That looks like this :

<xsl:if test=”$previousstartchar != $currentstartchar”>
   <A NAME=”{@Title}”></A>
</xsl:if>

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>