Transform Web Controls In Sitecore Rich Text Editor

August 25, 2011

This article is a follow-up to an article written previously by John West about embedding web controls in Rich Text Editors.

I spent some good time using web controls in the Rich Text Editor to allow content editors to implement complex functionality inline without a lot of strain. It's a step up from using code snippets where you don't want a content editor to have to switch to html mode to set html attributes or other behind-the-scenes elements.

For those of you not familiar with web controls or server controls it's basically a custom tag in the form <customTagPrefix:customTagName>. I use dialog windows to create them and, as long as your web.config setting "HtmlEditor.SupportWebControls" is set to true, the Sitecore Rich Text editor will display them with an icon like so:

 web control

But to make sure the tag isn't just spit out and treated as if it were any other html tag you'll need to transform it.

As I mentioned John West has already fully explained how to render fields by setting up a custom pipeline event handler. I ended up recently finding out that this case doesn't cover when you place the content field value on the page without using a Field Renderer. Let's say you have a field that, for whatever reason, you decided to place on a page as plain text by using it's Field.Value property. You'll find that the web controls won't be rendered and if you look at the html source you'll see them there staring you in the face plain as day. What you can do is add into your vast library of code, a utility method (pulled and modified from John West's post) that takes in the string value and transforms the web controls then returns a string value. This will turn all those fun webcontrols from boring html into amazing displays of genius. 

The method is as follows:

public static string TransformWebControls(string args) {	System.Web.UI.Page page = new System.Web.UI.Page();	page.AppRelativeVirtualPath = "/";	System.Web.UI.Control control = page.ParseControl(args);	return Sitecore.Web.HtmlUtil.RenderControl(control);}

Ok...Carry on.