More on Data View Web Parts

Add a DVWP to a sub-site, which is depend on a SP List in root site.

Add “WebUrl” DataFormParameter to the “DataSourcesString” Property as follows. Here by specifying DefaultValue="/" to the WebUrl parameter, web part will always refer to the SP List instance in rootsite.

<property name="DataSourcesString" type="string">
&lt;%@ Register TagPrefix="cc1" Namespace="Microsoft.SharePoint.WebControls"
&lt;%@ Register TagPrefix="cc2" Namespace="Microsoft.SharePoint.WebPartPages"
&lt;cc1:SPDataSource runat="server" UseInternalName="True" SelectCommand="....
&lt;cc2:DataFormParameter ParameterKey="ListName"
PropertyName="ParameterValues" DefaultValue="In The News"
&lt;cc2:DataFormParameter ParameterKey="WebUrl"
PropertyName="ParameterValues" DefaultValue="/"
&lt;asp:Parameter DefaultValue="5" Name="MaximumRows"&gt;

Even though a Web Part is designed to use only in the rootsite, adding “WebUrl” DataFormParameter is necessary, since SharePoint shares Web Part gallery across entire site collection. So adding “WebUrl” DataFormParameter will prevent generating an error when users try to add SP List dependent DVWP to a sub site.

Display Limited number of Characters from a Rich Text Content/Field
If the data field is a html field, it is not possible to directly apply substring() function as the field value contains html tags as well. So if we directly applied the substring(), actual number of characters it return is less than the specified amount due to HTMl tags.
Therefore we need to apply a HTML stripping method first to get the pure text that we need to substring.

1. Define a parameter specifying the number of characters to be displayed.
<ParameterBinding Name="CharactersLimit" Location="None" DefaultValue="300"/>

2. Define a XSL template to remove HTML tags.
<xsl:template name="removeHtmlTags">
<xsl:param name="html"/>
<xsl:when test="contains($html, '&lt;')">
<xsl:value-of select="substring-before($html, '&lt;')"/>
<!--Recursively call removeHtmlTags template -->
<xsl:call-template name="removeHtmlTags">
<xsl:with-param name="html" select="substring-after($html, '&gt;')"/>
<xsl:value-of select="$html"/>

3. Get the pure text content by calling “removeHtmlTags” template.
<xsl:variable name="pureText">
<xsl:call-template name="removeHtmlTags">
<xsl:with-param name="html" select="@myRichTextContent" />

4. Extracts the specified number of characters and display.
<xsl:when test="string-length($pureText) &amp; gt;number($CharactersLimit)">
<p class="paraStyle_01">
<xsl:value-of select="substring($pureText,1,number($CharactersLimit))"/>
<p class="paraStyle_01"><xsl:value-of select="$pureText"/></p>