Data View Web Part Tricks


One of the most handy out of the box customization components in my opinion is the Data View Web Part, but mucking around in XSLT can be a pain at times so here’s some reference points for making things easier, these work with SharePoint 2007/2010 and anything else that can use XSLT.

The first snippet is for pulling unique values down from your data source initially. The second is the much more fun way to make it so the DVWP filter drop downs are unique per column. The filter drop down unique value code snippet is put in to the template and is field type independent. Next we have an XSLT template for changing a string to be all uppercase to make a query comparison case-insensitive effectively, it can also be used for always showing a field as uppercase.

I also pasted some of my older XSLT tidbits related to truncated strings for Read More links and proper string extraction for rendering url and image fields.

<strong><span style="font-size: small;">Unique Values in initial query</span></strong>
<xsl:variable name="Rows" select="/dsQueryResponse/Rows/Row[not (@Title=preceding-sibling::Row/@Title)]" /> <strong> </strong>
<strong><span style="font-size: small;">Unique values in filter drop downs
</span></strong><xsl:when test="starts-with($fieldname, '@')"
<xsl:variable name="dvt_Rows">
<strong>Changed select from $Rows
</strong>lt;xsl:for-each select="/dsQueryResponse/Rows/Row[not(@*[name()=$dvt_FieldNameNoAtSign]=preceding-sibling::Row/@*[name()=$dvt_FieldNameNoAtSign])]">
<xsl:sort select="@*[name()=$fieldname]" order="ascending" data-type="{$sorttype}" />
<xsl:copy-of select="." />
<xsl:variable>
<strong><span style="font-size: small;">Case Insensitive Comparison (or change case)</span></strong>
<xsl:template name="ToUpper">
    <xsl:param name="StringValue"/>
    <xsl:value-of select="translate($StringValue, 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')"/>
<xsl:template>
<strong><span style="font-size: small;">Field Length Truncate (Read More link and fitting in long content)</span></strong>
<a href="@FileRef”>
 <xsl:variable name="truncate-length” select="40”/>
 <xsl:value-of select="substring(@Title,1, $truncate-length)”/>
 <xsl:if test="string-length(@Title) > $truncate-length”>
  <xsl:text>...
 xsl:if>
</a>
<strong><span style="font-size: small;">Properly format a URL or image field</span></strong>
Link:
<a href="{substring-before(@URL, ', ')}">
 <xsl:value-of select="substring-after(@URL, ', ')"/>
</a>
Image:
<img src="{substring-before(@Field, ', ')}" alt="{substring-after(@Field, ', ')}"/>
<strong>Strip a character from a field for comparison</strong>
contains(translate(@Case_x0020_Number, ',', ''), translate($Query, ',', ''))
In this case I did a comma-insensitive comparison, but replacing that , with whatever you want/need will work too! :)
  1. #1 by Nik on March 25, 2012 - 9:54 pm

    Thanks for the Post it really helped on the string-length function.

    Thanks …

(will not be published)


%d bloggers like this: