Sitecore – Encoding Special Characters In Fields
If web standards compliance is one of your top priorities and you have a site core implementation, then you may want to continue reading. Are you tired of seeing & instead of & on your site? Not sure of the best way to encode them? I have created a processor that snaps into the renderField pipeline. The only caveat is that in order for this code to run you need to use the FieldRenderer.Render(Item, FieldName) method. Which, in my opinion, you should always be using anyway.
You need to create a new class with a Process method like so:
using System.Text.RegularExpressions;
using Sitecore.Pipelines.RenderField;
namespace your.namespace.here
{
public class special_character_replacer
{
// Methods
public void Process(RenderFieldArgs args)
{
args.Result.FirstPart = Regex.Replace(args.Result.FirstPart, @"&(?!.{2,5};)(?!\w*=)", "&");
//More special character replacements can go here.
}
}
}
*** notice the first &. This is necessary as it's in the web.config and needs to be encoded, but will search for "&". Also, keep in mind that the Ampersand may already be encoded, don't replace them, and don't replace any ampersands in URLs.
Then you need to add the processor you just created to the renderField pipeline like so:
That's it. Now just sit back and watch the encoding begin.