Show SingleLine Text

Show SingleLine Text

Single Line Text Display

  • The <sc:text> XSL extension control is used to display the value of a Single-Line Text field or other simple text field in xslt templates
  • Specify the field name in double quotes – replace “fieldname” with the name of a field, such as “title”
  • Enclose the sc:text tag with tags that provide the required formatting

Controls featured in this example:

  • Sitecore Web Control
  • XSLT Rendering

Classes featured in this example:

  • Sitecore.Context
  • Sitecore.Data.Database
  • Sitecore.Data.Fields.TextField
  • Sitecore.Data.Items.Item

Show SingleLine Text C# Web Control Code

//get Simple Fields metadata item from context database using item guid
Sitecore.Data.Database db = Sitecore.Context.Database;
Sitecore.Data.Items.Item item = db.GetItem("{EC44470F-FF45-4023-95C2-EC886FD33D0C}");
if (item == null) return;

Sitecore.Data.Fields.TextField sltFld = item.Fields["example single-line text"];
if (sltFld == null) return;

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

output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-size:x-small; width:100%; clear:both");
output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-weight:bold");
//using &#58; for colon and &#160; for space
output.Write("{0}&#58;&#160;{1}", "Item", item.DisplayName);
output.RenderEndTag(); //P

output.Write("{0}&#58;&#160;{1}", sltFld.InnerField.DisplayName + " Field Value", "<b>" + sltFld.Value + "</b>" );
output.RenderEndTag(); //Div

Show SingleLine Text XSLT Rendering Code

<xsl:variable name="simpleFields" select="sc:item('{EC44470F-FF45-4023-95C2-EC886FD33D0C}',.)" />

<div style="font-weight:bold; width:100%; clear:both"> <br/>
<xsl:value-of select="'XSLT Rendering Real-time Output'"/>

<div style="font-size:x-small; width:100%; clear:both">
<p><b>Item: <xsl:value-of select="$simpleFields/@name"/></b></p>
<!-- alternative to <xsl:value-of select="sc:fld('example single-line text',$simpleFields)" /> -->
<xsl:value-of select="'Example Single-Line Text Field Value: '"/>
<b><sc:text field="example single-line text" select="$simpleFields" /></b><br/>

Show SingleLine Text C# Web Control Output

Example Single-Line Text Field Value: This raw text can fit on a single line

Show SingleLine Text XSLT Rendering Output

Example Single-Line Text Field Value: This raw text can fit on a single line