Smart TextToHTML

First things first: if you don’t know what I mean by “TextToHTML “, you won’t get much from this post. In this case, I recommend that you start with this introduction.
In short, the TextToHTML script has two roles:
1/ find HTML strings in a SharePoint page
2/ Convert these strings into actual HTML

Yesterday, I came across a discussion between @EUSP, @webdes03 and @ebrackley on Twitter:
“TextToHTML can be a bad performer when tasked with lots of HTML; also research DVWPs”

This is a timely comment, as I am about to present at SharePoint Saturday EMEA. Let me expand on this, and provide a few hints on how to better use the HTML Calculated Column.

Remember: TextToHTML is not the only way

Right, most of the examples you’ll find on my blog rely on the TextToHTML script, embedded in a Content Editor Web Part. But there are other ways to render the HTML. In particular, the Data View Web Part, used in crosslist mode, or the Content Query Web Part can directly do the rendering. For more information, see these series:
– for MOSS
KPI roll-up in MOSS
– for SharePoint 2007 (applies to both wss and MOSS):
KPI roll-up in SharePoint (Part I)
KPI roll-up in SharePoint (Part II)

The latter will be the theme of my presentation at SharePoint Saturday.

Get a faster browser

Not always a choice, but if you can, upgrade your browser to the latest version. These days, browsers are improving by leaps and bounds, with Google leading the charge.
My tests show that the TextToHTML script is four times faster in IE 8 than in IE 7. That’s huge! Some other browsers offer even better response time.

Faster TextToHTML

If you are using the TextToHTML script, you’ve certainly got the current version from the download section. What I am making available over there is a generic script, for both SharePoint 2003 and 2007. If you are on SharePoint 2007, you can make it more specific and thus faster.

Focus on the main content

As is, the script will scan the whole page to find table cells (TD elements):

TextToHTML(document.getElementsByTagName("TD"),regexpTD);

This is a waste of time, as you just need the main content, excluding header and Quicklaunch. You can easily do this by replacing the above line with the following code:

var theMainContent = document.getElementById("MSO_MainContent");
TextToHTML(theMainContent.getElementsByTagName("TD"),regexpTD);


Use selectors

Instead of grabbing all the cells in the document, try to restrict your scope to the cells that may contain HTML Calculated Columns.

For example, Paul Grenier proposed a jQuery version of my script, which allows you to grab only certain cells. If HTML Calculated Columns are only in List View Web Parts on your page, the simple $(“td.ms-vb2”) selector should be enough. For better performance, you may want to combine it with my first advice, and focus on the main content: $(“#MSO_MainContent td.ms-vb2”).

Here again, a modern browser will give you better performance (for example if it has native tools to find elements by class name).

Place your script close to the point of consumption

Imagine that you are displaying 10 lists on your page, but only the third one is using HTML calculated columns. If you place the CEWP that contains TextToHTML at the bottom of the page, the script will go through the 10 lists. If you place the script right below the third list: when the script runs, it will be faster as it only sees 3 lists, the 7 others being displayed later.
btw placing the scripts close to the point of consumption is a good practice that also applies to other scripts, not just here. In traditional Web design, you would find all the scripts under the head section of the page, but this practice has evolved now that pages are more dynamic.

If you have other ideas, feel free to share them with me and the other readers! I’ll push some of these performance improvements in the next release of the TextToHTML script. I am also looking for volunteers to test the beta versions…

Advertisements

10 thoughts on “Smart TextToHTML

  1. Pingback: uberVU - social comments

  2. Pingback: Links (1/17/2010) « Steve Pietrek-Everything SharePoint/Silverlight

  3. The TextToHTML is awesome but using it for gantt bars isn’t displaying the bars in FireFox. You mention having FireFox tips but I cannot locate them.

    Thanks.

    • Sorry, I missed that. Check my posts on progress bars, they work in Firefox and the same technique applied to the Gantt bars should work for you. I’ll try to fix it when I have a moment.

  4. Chris,

    You have some great stuff going here and I would be delighted to help test things out in my environment. Let me know how I can help.

  5. I am using calculated fields to apply a hyperlink over a numeric field, and using your JavaScript to render it as HTML. I have this calculated field with the JavaScript on a custom DataView I created where I use Conditional Formatting to apply a background color. For some reason the JavaScript seems to be interferring with the Conditional Formatting of the DataView page as it is not applying the Conditional Formatting to that calculated field.

    Do you know how I can get around this apparent interference?

    I know I can apply the background color to the calculated field, and I am doing that on the built in pages…but on this custom DataView page they need it to sort numerically therefore it can’t be a calculated field…thus one of the many reasons I have a custom DataView.

    • Linda, I am not sure why this happens, but unfortunately this is not surprising. TextToHTML expects text, and will ignore additional formatting. I’ll work on this for the next version.

    • You are correct, the current version of TextToHTML doesn’t work in these cases.

      As I mentioned in the post, you could try another approach for filtering and grouping, like the DVWP.

      Also note that all those methods only work for HTML. As expected they won’t work with “Export to Excel” and “Edit in spreadsheet”, which are not using HTML.

    • For Filtering, and Sorting you can…What I do is create my calculated fields, then in SharePoint Designer I convert the list view to a Data View. Then I change the fields to the data ENTRY fields, not the calculated fields so that the header sorting/filtering is running off the data entry fields.

      Then I click on the actual data cell in the column and change its focus to the calculated fields. This way the sorting and filtering still works as it is going off of the data entry fields, while the table is displaying the calculated fields.

      That is how I make it work.

Comments are closed.