Microsoft Dynamics CRM 4.0 - Thought Repository

Thursday, April 9, 2009

Clean Jumbled HTML from Email Activities Within Reports

View Comments


CRM 4.0 stores emails tracked from Outlook as HTML within the database. So, when you open up a tracked email activity within CRM, it looks exactly the same as it would in Outlook. Unfortunately this causes some problems when you try to query the CRM database for some custom SQL reporting services reports. Any queries that use the description column from the FilteredEmail or FilteredActivity views will come up as pure HTML, befuddling users when they open up a report.

The function below uses regular expressions to parse out the HTML tags that are an eyesore for your report users, leaving behind just the text of the email message.
Function RemoveHTML( strText )
 strText  = System.Text.RegularExpressions.Regex.Replace(strText, "\n", " ")
 strText  = System.Text.RegularExpressions.Regex.Replace(strText, "( )+", " ")
 strText  = System.Text.RegularExpressions.Regex.Replace(strText, "<( )*head([^>])*>","<head>", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
 strText  = System.Text.RegularExpressions.Regex.Replace(strText, "(<( )*(/)( )*head( )*>)","", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
 strText  = System.Text.RegularExpressions.Regex.Replace(strText, "(<head>).*()",string.Empty, System.Text.RegularExpressions.RegexOptions.IgnoreCase)
 strText  = System.Text.RegularExpressions.Regex.Replace(strText, "<( )*script([^>])*>","<script>", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
 strText  = System.Text.RegularExpressions.Regex.Replace(strText, "(<( )*(/)( )*script( )*>)","</script>", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
 strText  = System.Text.RegularExpressions.Regex.Replace(strText, "(<script>).*(</script>)",string.Empty, System.Text.RegularExpressions.RegexOptions.IgnoreCase)
 strText  = System.Text.RegularExpressions.Regex.Replace(strText, "<( )*style([^>])*>","<style>", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
 strText  = System.Text.RegularExpressions.Regex.Replace(strText, "(<( )*(/)( )*style( )*>)","</style>", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
 strText  = System.Text.RegularExpressions.Regex.Replace(strText, "(<style>).*(</style>)",string.Empty, System.Text.RegularExpressions.RegexOptions.IgnoreCase)
 strText  = System.Text.RegularExpressions.Regex.Replace(strText, "<( )*br( )*>",vbCrLf, System.Text.RegularExpressions.RegexOptions.IgnoreCase)
 strText  = System.Text.RegularExpressions.Regex.Replace(strText, "<( )*li( )*>",vbCrLf, System.Text.RegularExpressions.RegexOptions.IgnoreCase)
 strText  = System.Text.RegularExpressions.Regex.Replace(strText, "<( )*div([^>])*>",vbCrLf, System.Text.RegularExpressions.RegexOptions.IgnoreCase)
 strText  = System.Text.RegularExpressions.Regex.Replace(strText, "<( )*tr([^>])*>",vbCrLf, System.Text.RegularExpressions.RegexOptions.IgnoreCase)
 strText  = System.Text.RegularExpressions.Regex.Replace(strText, "<( )*p([^>])*>",vbCrLf, System.Text.RegularExpressions.RegexOptions.IgnoreCase)
 strText  = System.Text.RegularExpressions.Regex.Replace(strText, "<[^>]*>",string.Empty, System.Text.RegularExpressions.RegexOptions.IgnoreCase)
 strText  = System.Text.RegularExpressions.Regex.Replace(strText, "&nbsp;"," ", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
 strText  = System.Text.RegularExpressions.Regex.Replace(strText, "&bull;"," * ", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
 strText  = System.Text.RegularExpressions.Regex.Replace(strText, "&lt;","<", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
 strText  = System.Text.RegularExpressions.Regex.Replace(strText, "&gt;",">", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
 strText  = System.Text.RegularExpressions.Regex.Replace(strText, "&quot;","""", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
 strText  = System.Text.RegularExpressions.Regex.Replace(strText, "&lsaquo;","<", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
 strText  = System.Text.RegularExpressions.Regex.Replace(strText, "&rsaquo;",">", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
 strText  = System.Text.RegularExpressions.Regex.Replace(strText, "&trade;","(tm)", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
 strText  = System.Text.RegularExpressions.Regex.Replace(strText, "&frasl;","/", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
 strText  = System.Text.RegularExpressions.Regex.Replace(strText, "&amp;","&", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
 strText  = System.Text.RegularExpressions.Regex.Replace(strText, "&#8211;","–", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
 strText  = System.Text.RegularExpressions.Regex.Replace(strText, "&#8220;","""", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
 strText  = System.Text.RegularExpressions.Regex.Replace(strText, "&#8221;","""", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
 strText  = System.Text.RegularExpressions.Regex.Replace(strText, "&#8216;","'", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
 strText  = System.Text.RegularExpressions.Regex.Replace(strText, "&#8217;","'", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
 RemoveHTML = strText
End Function
Navigate to the layout tab of your report and right-click the whitespace around the report to modify the properties. Place the code inside the Code tab of the Report Properties as shown below.

To call your function within the layout page of your report, use the expression "=code.RemoveHTML(Fields!Description.Value)" where Description is the name of the column that holds the HTML email message body.

blog comments powered by Disqus

About Me

Henry Bow
I'm a programmer living in sunny Orange Country, California. Since the beginning of 2008, I've been developing on the MS Dynamics CRM 4.0 platform. This blog will help me jot down some of the tips and neat features I've developed along the way while giving me a chance to dabble into the curious world of analytics and SEO.

Please let me know if I can help with your CRM needs.
hbow27@gmail.com

Feed Rss

Subscribe to new posts via e-mail

Recent Posts