Using GetString

Using GetString

Controls featured in this example:

  • Sitecore Web Control

Classes featured in this example:

  • Sitecore.Collections.FieldCollection
  • Sitecore.Context
  • Sitecore.Data.Fields.Field
  • Sitecore.Data.Items.Item
  • Sitecore.StringUtil

Using GetString C# Web Control Code

Sitecore.Data.Items.Item item = Sitecore.Context.Item;
if (item == null) return;

Sitecore.Collections.FieldCollection fields = item.Fields;
if (fields == 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);

string displayName = Sitecore.StringUtil.GetString(item.DisplayName, item.Name);
//using : for colon and   for space
output.Write("{0}: {1}", "Item", displayName);
output.RenderEndTag(); //P

foreach (Sitecore.Data.Fields.Field field in fields)
{
    //using two overloads of GetString method
    string title = Sitecore.StringUtil.GetString(field.Title, field.Name);
    string type = Sitecore.StringUtil.GetString(field.Type);
    output.Write("{0}&#58;&#160;{1}{2}", "Field", "<b>" + title + "</b> - ", type + "<br/>");
}
output.RenderEndTag(); //Div


Using GetString C# Web Control Output

Field: Created by - text
Field: Web Control Demo - Rich Text
Field: Updated by - text
Field: Abstract - Multi-Line Text
Field: Revision - text
Field: Menu Title - Single-Line Text
Field: Title - Single-Line Text
Field: Info - Rich Text
Field: CSharp Web Control Code - Rich Text
Field: Image - Image
Field: Breadcrumb Title - Single-Line Text
Field: Related Topics - Multilist
Field: Created - datetime
Field: Menu Tooltip - Single-Line Text
Field: Updated - datetime
Field: Display name - text
Field: Sortorder - text