Data View Web Part Tricks

Slalom Consultant Maarten Sundman

Slalom Consultant Maarten Sundman specializes in .NET, SharePoint, and Silverlight solutions and has experience in the Financial Services, Software, and Utilities and Energy sectors.

by Maarten Sundman

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.

Unique Values in initial query
<xsl:variable name="Rows" select="/dsQueryResponse/Rows/Row[not (@Title=preceding-sibling::Row/@Title)]" />
Unique values in filter drop downs <xsl:when test="starts-with($fieldname, '@')"
<xsl:variable name="dvt_Rows">
Changed select from $Rows 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>
Case Insensitive Comparison (or change case)
<xsl:template name="ToUpper">
    <xsl:param name="StringValue"/>
    <xsl:value-of select="translate($StringValue, 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')"/>
<xsl:template>
Field Length Truncate (Read More link and fitting in long content)
<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>
Properly format a URL or image field
Link:
<a href="{substring-before(@URL, ', ')}">
 <xsl:value-of select="substring-after(@URL, ', ')"/>
</a>
Image:
<img src="{substring-before(@Field, ', ')}" alt="{substring-after(@Field, ', ')}"/>

– Maarten

5 Responses to Data View Web Part Tricks

  1. Pingback: Data View Web Part Tricks « xsl

  2. raki says:

    Hello Marteen, I have issue with filter drop down, I want to get unique values in DVWP person field (Sharepoint 2010 grouping toolbar is added). Please let me know how to achieve this.

    • Maarten Sundman says:

      You can do this by using a custom row filter with the filter drop down template instead of using $rows. I don’t have a sample handy to demo this to you though. Use preceding sibling to get the previous row and if it doesn’t match show it. This requires ordering by the filter column when you are handling the drop down.

  3. kriss says:

    Hi, i need to display the Images and Fileds from picture library into dataview webpart in horizontal manner so please let me know the solution for this requirement as it is urgent!!
    regards
    kriss

  4. Snehal Rana says:

    Hi Marteen,
    Great article !! Quick question, How do I create a filter for data view web part to show only values based upon user’s profile property. In my case I need to filter the list based upon the user’s profile property. Let me know
    Regards,
    Snehal

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: