Using FieldRenderer

Using FieldRenderer

Field Types Supported by the FieldRenderer Class

  • Single-Line Text and Multi-Line Text fields
  • Integer and Number fields
  • Date - Rendered html will be a formatted date
  • Datetime - Rendered html will be a formatted date and time
  • General Link - Will render an appropriate html tag
  • Rich Text - Internal links (dynamic links) will be expanded Image fields (will render an html tag. If parameters are used to resize, the image will be resized on the server, cached and the resized image will be sent to the client)
  • Word Document - An Add-on to Internet Explorer is used if Word is installed

FieldRenderer Web Control

  • Retrieves, formats, and outputs a single field value
  • Used to display a field value directly on a sublayout without coding
  • It can be dragged onto a layout or sublayout or bound to a placeholder
  • Inherits from the FieldRenderer class
  • Rather than raw values, will render appropriate html code for some data types
  • Calls the renderField pipeline and expands dynamic links
  • Generates inline editing controls for the Page Editor
  • Does not support output caching options
  • To support output caching, create a Web control that inherits from the FieldRenderer Web control (Sitecore.Web.UI.WebControls.FieldRenderer) and define the GetCachingID()method to return a cache key for the control

Controls featured in this example:

  • Sitecore Web Control

Classes featured in this example:

  • Sitecore.Data.Database
  • Sitecore.Data.Items.Item
  • Sitecore.Web.UI.WebControls.FieldRenderer

Using FieldRenderer C# Web Control Code

Sitecore.Data.Database db = Sitecore.Context.Database;
Sitecore.Data.Items.Item item = db.GetItem("{EC44470F-FF45-4023-95C2-EC886FD33D0C}");
if (item == null) return;

output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-weight:bold; width:100%; clear:both");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Div);
output.Write("C# Web Control Real-time Output");
output.RenderEndTag();

output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-size:x-small; width:100%; clear:both");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Div);
output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-weight:bold");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.P);
output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Name, "Name");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.A);
output.Write("{0}: {1}", "Item", item.DisplayName);
output.RenderEndTag(); //A
output.RenderEndTag(); //P

output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Border, "1");
output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Cellpadding, "3");
output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-size:smaller");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Table);
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Thead);
output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Align, "center");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Th);
output.Write("Field Type");
output.RenderEndTag(); //Th
output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Align, "center");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Th);
output.Write("Field Value Display");
output.RenderEndTag(); //Th
output.RenderEndTag(); //Thead
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Tbody);

output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Tr);
output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Align, "left");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Td);
output.Write("{0}", "Date Field");
output.RenderEndTag(); //Td

output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Align, "left");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Td);
output.Write("{0}", Sitecore.Web.UI.WebControls.FieldRenderer.Render(item, "example date", "format=MM/dd/yy") + "</b>");
output.RenderEndTag(); //Td
output.RenderEndTag(); //Tr

output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Tr);
output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Align, "left");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Td);
output.Write("{0}", "Datetime Field");
output.RenderEndTag(); //Td

output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Align, "left");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Td);
output.Write("{0}", Sitecore.Web.UI.WebControls.FieldRenderer.Render(item, "example datetime", "format=MM-dd-yyyy hh:mm:ss") + "</b>");
output.RenderEndTag(); //Td
output.RenderEndTag(); //Tr

output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Tr);
output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Align, "left");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Td);
output.Write("{0}", "Image Field");
output.RenderEndTag(); //Td

output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Align, "left");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Td);
output.Write("{0}", Sitecore.Web.UI.WebControls.FieldRenderer.Render(item, "example image", "mh=60") + "</b>");
output.RenderEndTag(); //Td
output.RenderEndTag(); //Tr

output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Tr);
output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Align, "left");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Td);
output.Write("{0}", "Integer Field");
output.RenderEndTag(); //Td

output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Align, "left");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Td);
output.Write("{0}", Sitecore.Web.UI.WebControls.FieldRenderer.Render(item, "example integer") + "</b>");
output.RenderEndTag(); //Td
output.RenderEndTag(); //Tr

output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Tr);
output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Align, "left");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Td);
output.Write("{0}", "Multi-Line Text Field");
output.RenderEndTag(); //Td

output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Align, "left");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Td);
output.Write("{0}", Sitecore.Web.UI.WebControls.FieldRenderer.Render(item, "example multi-line text") + "</b>");
output.RenderEndTag(); //Td
output.RenderEndTag(); //Tr

output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Tr);
output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Align, "left");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Td);
output.Write("{0}", "Number Field");
output.RenderEndTag(); //Td

output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Align, "left");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Td);
output.Write("{0}", Sitecore.Web.UI.WebControls.FieldRenderer.Render(item, "example number") + "</b>");
output.RenderEndTag(); //Td
output.RenderEndTag(); //Tr

output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Tr);
output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Align, "left");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Td);
output.Write("{0}", "Password Field");
output.RenderEndTag(); //Td

output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Align, "left");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Td);
output.Write("{0}", Sitecore.Web.UI.WebControls.FieldRenderer.Render(item, "example password") + "</b>");
output.RenderEndTag(); //Td
output.RenderEndTag(); //Tr

output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Tr);
output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Align, "left");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Td);
output.Write("{0}", "Rich Text Field");
output.RenderEndTag(); //Td

output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Align, "left");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Td);
output.Write("{0}", Sitecore.Web.UI.WebControls.FieldRenderer.Render(item, "example rich text") + "</b>");
output.RenderEndTag(); //Td
output.RenderEndTag(); //Tr

output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Tr);
output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Align, "left");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Td);
output.Write("{0}", "Single-Line Text Field");
output.RenderEndTag(); //Td

output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Align, "left");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Td);
output.Write("{0}", Sitecore.Web.UI.WebControls.FieldRenderer.Render(item, "example single-line text") + "</b>");
output.RenderEndTag(); //Td
output.RenderEndTag(); //Tr

output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Tr);
output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Align, "left");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Td);
output.Write("{0}", "Word Document");
output.RenderEndTag(); //Td

output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Align, "left");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Td);
output.Write("{0}", Sitecore.Web.UI.WebControls.FieldRenderer.Render(item, "example word document") + "</b>");
output.RenderEndTag(); //Td
output.RenderEndTag(); //Tr

output.RenderEndTag(); //Tbody
output.RenderEndTag(); //Table
output.RenderEndTag(); //Div


Using FieldRenderer C# Web Control Output

Field Type Field Value Display
Date Field
Datetime Field 07-05-2010 03:30:00
Image Field
Integer Field 997
Multi-Line Text Field This raw text cannot fit on a single line. The multi-line text field enables users to view the first few lines of the content immediately. Additional lines can be viewed by using the scroll bar.
Number Field 999.97
Password Field password
Rich Text Field

A rich text field can contain...

Single-Line Text Field This raw text can fit on a single line
Word Document

This content

was created

using a

Word Add-on

to Internet

Explorer