Pat’s .Net and Sharepoint Blog
In the Microsoft Sharepoint and .Net Trenches

Inserting Line breaks in a Sharepoint Data View Column

April 26th, 2008 . by <Patrick/>

Sometimes you want to fomat your Data View columns in a special way but Sharepoint Designer dissapoints you. The answer is to get dirty and modify the XSL in the Code behind the Data View web part.

I had a requirement to display a delimitted string comming from a column on multiple lines something like this:

Name Description URLS
Patrick Web Monkey http://www.microsoft.com
http://www.12thwave.com
http://blog.12thwave.com/

In the list the URL column was a pipe delimitted string (from Infopath) that looked like this:

http://www.microsoft.com%7chttp//www.12thwave.com%7Chttp://blog.12thwave.com

There are several XPath functions that could be used but unfortunately Sharepoint only supports XPath version 1.0 and all the good functions are in version 2.0 like tokenize() and split()

At first I tried the following technique (creating an xml file and adding it as a secondary data connection but for some reasone it did not like the hyperlinks I had) see: Inserting line breaks into text using Rules 

 

I came across an excellent site that provided me with a bunch of great xsl string functions where I found the solution:

http://www.dpawson.co.uk/xsl/sect2/N7240.html#d11074e349

Create a data view and use the following template
<xsl:template name=”links”>
  <xsl:param name=”str”/>
  <xsl:choose>
    <xsl:when test=”contains($str,’|')”>
      <a href=”{substring-before($str,’|')}” mce_href=”{substring-before($str,’|')}”><xsl:value-of
select=”substring-before($str,’|')”/></a>
      <xsl:text> </xsl:text><br/>
      <xsl:call-template name=”links”>
        <xsl:with-param name=”str” select=”substring-after($str,’|')”/>
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <a href=”{$str}” mce_href=”{$str}”><xsl:value-of select=”$str”/></a>
      <xsl:text> </xsl:text><br/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>
Then output your URL field like so:
<td>
 <xsl:call-template name=”links”>
 <xsl:with-param name=”str” select=”substring-after(@URL, ‘, ‘)” />
 </xsl:call-template>
</td>