Thursday, July 7, 2011

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"
Assembly".....
&lt;%@ Register TagPrefix="cc2" Namespace="Microsoft.SharePoint.WebPartPages"
Assembly=".....
&lt;cc1:SPDataSource runat="server" UseInternalName="True" SelectCommand="....
&lt;SelectParameters&gt;
&lt;cc2:DataFormParameter ParameterKey="ListName"
PropertyName="ParameterValues" DefaultValue="In The News"
Name="ListName"&gt;&lt;/cc2:DataFormParameter&gt;
&lt;cc2:DataFormParameter ParameterKey="WebUrl"
PropertyName="ParameterValues" DefaultValue="/"
Name="WebUrl"&gt;&lt;/cc2:DataFormParameter&gt;
&lt;asp:Parameter DefaultValue="5" Name="MaximumRows"&gt;
&lt;/asp:Parameter&gt;
&lt;/SelectParameters&gt;
&lt;/cc1:SPDataSource&gt;
</property>

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:choose>
<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:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$html"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

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" />
</xsl:call-template>
</xsl:variable>

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

No comments: