Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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

share|improve this question

1 Answer 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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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