1

I am creating a custom webpart to display the content of an announcement list similar to that of the Summary View of List View webpart. However I am not able to limit the character length of announcement body to 250 characters due to the presence of rich content in the body. Due to the project requirement creating a custom webpart is essential and I cannot use the OOB list view webpart. Is there a way to achieve similar functionality through code? Any help is highly appreciated.

Thanks in advance, Ankita

1

You can use a method like below. This strips the HTML from your content, and displays plain text.

<xsl:template name="removeHtmlTags">
    <xsl:param name="html"/>
    <xsl:choose>
        <xsl:when test="contains($html, '&lt;')">
            <xsl:value-of select="substring-before($html, '&lt;')"/>
            <!-- Recurse through HTML -->
            <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>

Then you setup a variable which cleans the text:

<xsl:variable name="pureText">
    <xsl:call-template name="removeHtmlTags">
        <xsl:with-param name="html" select ="@Body" />
    </xsl:call-template>
</xsl:variable>

and finally, call this new variable and trim it down

<xsl:value-of select="substring($pureText, 0, 250)" />

HTH

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.