SSRS and HTML rendering of ordered list

Microsoft’s SQL Server Reporting Services supports rendering of HTML tags, but for some reason that support stuck in 1990s – only very limited set is supported. And even using that set is problematic.

Case in point – ordered list. While officially supported – the way it is rendered is the stuff nightmares are made off. Jumble of original tags generously intermixed with DIVs and SPANs – it’s a wonder it renders at all.

And sometimes it doesn’t. If you try to view a report in Internet Explorer (especially from older, but still actively used versions of SSRS like 2008) numbering get screwed.

Let’s say you have following HTML in your report field (e.g. expression in a Text field):

  <li>Item 1</li>
  <li>Item 2</li>
  <li>Item 3</li>

You would expect this to render as

1. Item 1
2. Item 2
3. Item 3

Well, surprise. Due to convoluted rendering in IE the above may show as

1. Item 1
1. Item 2
1. Item 3

And since it’s mixed pile of tags – at this point there’s nothing you can to rendered HTML.

Fortunately you can intercept the problem at the bud. SSRS allows you to create custom function, by writing Visual Basic code. This gives us a chance to transform ordered list markup in something simple that SSRS won’t choke on. Consider the following:

Public Function UnList(str As String, Level as Integer) 
   Dim I as Integer = str.ToUpper.IndexOf("<LI>")
   If I = -1
	Return str
	Return str.Substring(0, I) & UnList(Level.ToString() & ". " & str.substring(I+4), Level + 1)
   End If
End Function
Function DeList(str As String)
  Return UnList(str.Replace("<OL>", "<DIV>").Replace("<ol>", "<div>").Replace("</OL>", "</DIV>").Replace("</ol>", "</div>").Replace("</li>", "<br/>").Replace("</LI>", "<BR/>"), 1)
End Function

Here main function DeList accepts our initial HTML as a parameter. It replaces <OL> tag with DIV wrapper and replaces </LI> tag with line break. Then it recursively calls helper function UnList that via series of recursive calls replaces starting <LI> tag with numeric sequence. If we pass our example HTML to this – it will become

  1. Item 1<br/>
  2. Item 2<br/>
  3. Item 3<br/>

Which even SSRS will always render correctly.

Note that this solution assumes there’s only one ordered list in your field of data, for multiple ones more advanced one will be needed.

Leave a Reply

Your email address will not be published. Required fields are marked *