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

When you post a long annoucement the webpart trims it to the first two lines but preserves any links in the html.

I'm trying to create a custom webpart that trims long text and want to reuse this functionality. Does anyone know where in the sharepoint libraries it lies?

I've tried looking through the source of ListItemWebPart in reflector, but i'm damned if i can work out where the trimming takes place.

edit - ideally i need to be doing this in c# code, rather than xsl

share|improve this question

2 Answers 2

What sort of webpart are you trying to use this in? If your using a Content Query WP or DataView then you could use a technique like Waldek posted about.

share|improve this answer
    
i'm trying to create a union of announcements and discussions around the site. the problem with waldek's technique is that it strips the html. the technique the annoucements wepart is that it keeps the links working, even if it trims the text off half way through one. –  user682 Apr 23 '10 at 11:40

I'm pretty sure it uses a COM object to render this. In XSLT they use the ddwrt:limit function where the code looks like this:

public string Limit(string inputText, int maxLength, string additionalText)
{
    if (inputText.Length > maxLength)
    {
        return (inputText.Substring(0, maxLength) + additionalText);
    }
    return inputText;
}

I'm sure you could write your own XSLT function that preserves the HTML and then do your own XSLT transformation of the data. I've written my own webpart which I do my own XSLT transformations of the list data instead of using the DataViewWebPart. I used reflector and snagged the DdwRuntime into my own class where I can add new functions that I can use in XSLT. Here's a code snippet.

XsltArgumentList xslArgs = new XsltArgumentList();
DdwRuntime runtime = new DdwRuntime();
runtime.View = view;
runtime.List = list;
runtime.Web = web;
runtime.ListItem = listItem;
xslArgs.AddExtensionObject("http://schemas.microsoft.com/WebParts/v2/DataView/runtime", runtime);

XslCompiledTransform transform = GetTransform();

using (StringWriter writer = new StringWriter())
{
    transform.Transform(content, xslArgs, writer);
    return writer.ToString();
}

But to be honest, this might be overkill for what you want.

I think just using some regular expressions you can get all html elements and make sure you only count real displayed text and make sure you only cut off displayed text.

share|improve this answer
    
Sorry about all of the XSLT stuff, I now see that you are writing a custom webpart specifically for this. ;-) –  Steve Lineberry Apr 23 '10 at 13:40

Your Answer

 
discard

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