Tag Archives: fieldrenderer

Using field item id to render field contents in Sitecore

Did you know that you can use the field item id for use with the FieldRenderer server control in the FieldName attribute?  For instance, take a look at this sample template item:

Sample Template

Sample Template where “Single Line Text Field” item id is {D2775315-00DC-4CF4-8B68-E9748127D188}

<sc:FieldRenderer ID=”frSingleLineText” runat=”server” FieldName=”{D2775315-00DC-4CF4-8B68-E9748127D188}” Item=”<%# Sitecore.Context.Item %>”></sc:FieldRenderer>

Why would you do this? Well, instead of having to memorize field name you can simply use the field item id.  But a much better reason is because if anyone ever changes the field name then the FieldRenderer will not work whereas if you put the field item id it will still continue to work regardless of how many times someone changes the field name.  You want to stay away from using Field names altogether in your code and stick to using id’s wherever possible.  This is why GlassMapper and other ORM tools are great because they do much of the same similar thing by wrapping up the fields so you can access the template as an object and the fields as a property.  However, sometimes you have to use the traditional Sitecore API to get things done and this is one way to keep code robust.

You can also do this as well in the code behind for a sample Literal control that is going to render the Single Line Text Field contents:

litSingleLineText.Text = myItem[“{D2775315-00DC-4CF4-8B68-E9748127D188}”];

This would require a compile in the solution though.  A better solution is to add the item in the HTML markup and you have a tag that is completely in the presentation and doesn’t need to be compiled when changed.

<sc:FieldRenderer ID=”frSingleLineText” runat=”server” FieldName=”{D2775315-00DC-4CF4-8B68-E9748127D188}” Item=”<%# Sitecore.Context.Item %>”></sc:FieldRenderer>

FieldRenderer and EnclosingTag property

So I was working on a sublayout earlier today and I ran across a piece of HTML that made me say, “Hmmm…, what is the best way to tackle this situation”?  I had the HTML below to work with for the “Page Title” and “Sub Title” fields that were to be simply rendered in a FieldRenderer control.

<h1>My Account <small>Manage your account online</small></h1>

The title “My Account” would always exist but the “Manage your account online” in the <small> element would be optional for the Content Editor to use to display sub-titles.  Well, I am a programmer who likes to leave no markup behind so I quickly remembered the EnclosingTag property of the FieldRenderer that made perfect sense in this situation.  Below is the markup I used to pull in the fields:

<sc:FieldRenderer runat="server" FieldName="Page Title" ID="frPageTitle"></sc:FieldRenderer>
<sc:FieldRenderer runat="server" FieldName="Sub Title" ID="frSubtitle" EnclosingTag="small"></sc:FieldRenderer>

Instead of leaving the <small> HTML element in the markup on the page if the “Sub Title” field is blank, I used the EnclosingTag property to ensure that if there is a value to render it inside the <small> HTML element. However, if the field is blank then don’t display the <small> HTML element either. Pretty slick when you need it sometimes! Happy coding!