Recently I was working on a Sitecore website that was started by previous developers and I discovered a very annoying bug. When inserting links in a RichText field using the InsertSitecoreLink button, the links failed to translate to the user-friendly URL's when Published to the content delivery site.
Let me illustrate what I mean.
Edit the RichText field using the built-in Sitecore Editor:
Highlight a piece of text and click the InsertSitecoreLink button. Then select some content item from your site you wish to link to.
Normally that would have placed a piece of HTML on the field that would look like this:
<a href="~/link.aspx?_id=7E50AB0CACF343DC857ED53627BC1106&_z=z">Test Link</a>
And when Published to the content delivery site, that would translate into a more friendly URL:
<a href="/test/test.aspx">Test Link</a>
But instead I would still get the unfriendly link at the content delivery site:
<a href="~/link.aspx?_id=7E50AB0CACF343DC857ED53627BC1106&_z=z">Test Link</a>
About drove me bananas for a while. I'd done this several times before and never had the links failed to render correctly. I was starting to think that perhaps there was bug in the most current version of Sitecore.
That's when I hit upon a discovery that I thought I'd share.
The content was being rendered by a custom Rendering that a previous developer had created. It was a class that inherited from WebControl and looked like this:
public
class
InteriorContent : Sitecore.Web.UI.WebControl
{
protected
override
void DoRender(HtmlTextWriter output)
{
Item currentPage = Sitecore.Context.Item;
output.Write("<h1>" + currentPage["header title"] + "</h1>");
output.Write(currentPage["description"]);
}
}
While this works, for the most part, the problem lies in the rendering of the Rich Text field "description."
The links come out all wrong.
The solution I opted for was quite simple. Replace the custom rendering with a simple Sublayout containing a couple of Sitecore FieldRenderers. The FieldRenderers do a great job of parsing the content of the RichText field and replacing the URL's properly. Simply fetching the contents of the field and writing it to the HTML writer will not work.
Here is what the final control looked like:
<%@
register
TagPrefix="sc"
Namespace="Sitecore.Web.UI.WebControls"
Assembly="Sitecore.Kernel"
%>
<%@
Control
Language="c#"
AutoEventWireup="true"
TargetSchema="http://schemas.microsoft.com/intellisense/ie5"
%>
<h1><sc:fieldrenderer
runat="server"
renderingid="{E1AF4AA3-3B5D-4611-8C71-959AD261E5B7}"
id="FieldRenderer1"
fieldname="Header Title"></sc:fieldrenderer></h1>
<sc:fieldrenderer
runat="server"
renderingid="{E1AF4AA3-3B5D-4611-8C71-959AD261E5B7}"
id="FieldRenderer2"
fieldname="Description"></sc:fieldrenderer>
So in the end, it turned out not to be a bug after all, but a coder error, after all. And served as a reminder, that sometimes, a little digging goes a long way.
If you ever run into this, now you have a workaround.