Project Lifecycle

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 asp.net 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.

Comments

Rick Cabral
June 12, 2012
Mark, wondering what your experience has been embedding WebControls into the RTE in 6.5... Initial experiments seem to indicate this functionality no longer exists.
mark stiles
June 13, 2012
Hey Rick, as far as I can tell a stock 6.5 will support it. The web.config setting is still there: htmleditor.supportwebcontrols. Also if I go into Sitecore and add a web control in an html editor field and switch to design mode the css works to show the control. You may have to add a pipeline handler to support it. John West has a good article about setting it up: http://sitecorejohn.wordpress.com/2010/04/28/embed-renderings-maps-media-etc-in-sitecore-rich-text-editor-fields/
Rick Cabral
June 29, 2012
Upgraded a 6.3 installation to 6.5 update 4, and although the pipeline handler is in place, snippets inserted into the RTE disappear into the HTML and are not rendered as the traditional yellow box.
Rick Cabral
June 29, 2012
According to John West, this feature does not work in Sitecore 6.4.x and above, because of the newer implementation of the Telerik RAD editor. If you need it, he suggests (as do I) that you file a tech support incident with Sitecore. I'm also going to use my MVP powers to lobby for this one.