I wish I could tell you I’ve discovered the panacea to extirpate common issues inherent in many Sitecore development projects and this article would enumerate a recipe for curtailing these, or how I’ve discovered a hidden gem within Sitecore.Kernel.dll that your future projects cannot live without.
Sadly, this article will do none of these things — it will not knock your socks off, and may even leave you saying to yourself “come on Mike, tell me something I don’t already know”.
Yet, I feel adamantly compelled to pen this article given an egregious practice I’ve seen employed by numerous Sitecore developers at multiple Sitecore development shops over the span of five years.
What is this foul practice? It’s the act of building HTML in your C# code using content from Sitecore fields and displaying this HTML via ASP.NET Literal or Label Web Controls.
No doubt, you have seen some code like the following in a Sitecore solution, or have even written code like this yourself (please note these code examples haven’t been tested, aren’t robust, and are only given as pedagogical illustrations):
An ASP.NET User Control containing a Literal Web Control:
Code-behind for the User Control:
In the above code-behind, the author composed some link HTML using content within a Sitecore General Link field named ‘My Link’.
Compare that with the following:
An ASP.NET User Control containing Sitecore Link Field Control:
Code-behind for the User Control:
Notice how much less code was needed to wire-up the Link Field Control. The Link Field Control will render link HTML if the current Item has a link set on the ‘My Link’ General Link field — including the target attribute, if a target is set on the General Link field.
You might be saying “Mike, this is great, but we don’t want this working in the Page Editor — we want this field to be completely locked down since it’s in a global Item”. Well, you could use the following to disable your Field Control in the Page Editor, and have the ability to change it later without having to do a full code build (assuming you’re using the Web Application model in ASP.NET):
If you are uncertain about which Field Control to use, you could always poke around in Sitecore.Web.UI.WebControls using .NET Reflector, or could opt to use a FieldRenderer Web Control — all Sitecore Field Controls use an instance of a FieldRenderer to output their HTML:
Using a FieldRenderer would also aid in allowing you to change a field’s type later without breaking its presentation in the UI — as long as the field type can be rendered by the FieldRenderer.
That’s it for now. Happy coding!
Good start. 🙂
You make an excellent point here about using the Field Renderer where possible to do the hard work. My complaint about this approach with link fields is that when translating, you often want to translate the link *text*, but not the link itself. If you allow Sitecore to render the whole link, and don’t split off the link text into its own field, it’s much more difficult to translate, and the link URL has to be duplicated across language layers.
I see your point. Have you ever thought about creating a custom Field Control to accomplish this?
[…] a rendered Sitecore page, I encountered something I had seen in the past but forgot about: Sitecore FieldControls and the FieldRenderer Web Control will convert newlines into HTML […]