Monday, February 16, 2015

Enable HTML tags in Single-line or Multi-line text fields - an oldie but a goodie

This comes up time and time again. When upgrading older 6.x Sitecore solutions to 7 and now 8, some content areas seem to be broken because html tags start being rendered on pages.


The GetTextFieldValue processor that was added to the renderField pipeline in 7 and above ensures that all text fields are output escaped when rendered through the renderField pipeline.

Yes, this is actually a really good security mechanism. However, if you have tech savvy Content Authors, you may want to turn this off.

This processor will help you do just that.


The key is the HttpUtility.HtmlDecode on line 20 below:

1:  using System.Web;  
2:  using Sitecore.Diagnostics;  
3:  using Sitecore.Pipelines.RenderField;  
4:  namespace MyProject.Framework.Pipelines.RenderField  
5:  {  
6:    /// <summary>  
7:    /// The get text field value.  
8:    ///   
9:    /// </summary>  
10:    public class CustomGetTextFieldValue  
11:    {   
12:      public void Process(RenderFieldArgs args)  
13:      {  
14:        Assert.ArgumentNotNull((object)args, "args");  
15:        string fieldTypeKey = args.FieldTypeKey;  
16:        if (fieldTypeKey != "text" && fieldTypeKey != "single-line text")  
17:        {  
18:          return;  
19:        }  
20:        args.Result.FirstPart = HttpUtility.HtmlDecode(args.Result.FirstPart);  
21:      }  
22:    }  
23:  }  

Config file for your include folder

 <configuration xmlns:patch="">  
    <processor type="MyProject.Framework.Pipelines.RenderField.CustomGetTextFieldValue, MyProject.Framework"  
       patch:instead="processor[@type='Sitecore.Pipelines.RenderField.GetTextFieldValue, Sitecore.Kernel']" />  


Unknown said...

Nice. That's a good little fix. I've had just this issue on a huge Sitecore 6.6 with a whole pile of content that I'm in the process of upgrading.

Martin English said...

Great stuff Andrew. Hope your upgrade went well!

Post a Comment