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

List consists from Title (Single line of text) and Content (Multiple lines of text - Enhanced rich text) columns.

I need to get snippet from Content column(~125 first symbols). I've found that text in Content column formatted and contains html tags. I want to know, how I can programmatically get plain text from rich text? I know that I can use Regex, but it's not nice solution.

Have an answer?

share|improve this question
add comment

3 Answers

If you dont want to use this

string content = Regex.Replace(textWithHTML, "<.*?>", string.Empty);

you can use this, this returns the SPListItem as normal text

private string getFieldAsText(SPListItem item, string fieldName, string captionName)
{
            return ReferenceEquals(item[fieldName], null) ? string.Empty :
                item.Fields[captionName].GetFieldValueAsText(item[fieldName]);
}
share|improve this answer
add comment

Didn't test, but you can probably use SPFieldMultiLineText.GetFieldValueAsText Method :

SPListItem yourItem = ....; // obtain the SPListItem with any method
SPFieldMultiLineText field = (SPFieldMultiLineText)yourItem.Fields["Content"]; 

string text = field.GetFieldValueAsText (yourItem["Content"]);
share|improve this answer
add comment

This is probably what you need:

const string htmlRegex = "<.*?>";

static string StripHTML (string inputString)
{
   // Removes all the HTML tags and returns the string with the 125 first characters
   return Regex.Replace(inputString, htmlRegex, string.Empty).Remove(125);
}
share|improve this answer
    
is there a solution without further processing? I mean is there method extracting plain text from SPListItem? –  Nurlan Jan 12 '12 at 10:39
    
@Nurlan Not that I am aware of, no. –  Daniel Ziga Jan 12 '12 at 10:41
    
@Nurlan Yes, see the answers. While this might work, I prefer the other methods. –  Kit Menke Jan 12 '12 at 15:58
add comment

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.