Friday, May 22, 2009

SPHttpUtility, a hidden gem

Recently we converted a Rich text field to a plain text field in a content type which resulted in raw html tags messed up with the plain text. There are lot of options to clean a html text to a plain text. We can use Regex, a custom approach etc.

But there is an OOTB way to do it which is hidden in SPHttpUtility class. It has a method called SPHttpUtility.ConvertSimpleHtmlToText which accepts an input html string and max length of input string. This class is specific to WSS Specific Rich text field as per the documentation. Specifying -1 to the max length property will make no maximum length.

We can use this method across the site to update all metadatas to clean up the html strings

This SPHttpUtility is coming in Microsoft.Sharepoint.Utilities namespace more information can be found in MSDN

No comments: