Check Field Values

Check Field Values

Controls featured in this example:

  • Sitecore Web Control

Classes featured in this example:

  • Sitecore.Configuration.Factory
  • Sitecore.Data.Database
  • Sitecore.Data.Fields.Field
  • Sitecore.Data.Items.Item
  • Sitecore.StringUtil

Check Field Values C# Web Control Code

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

Sitecore.Data.Items.Item parent = item.Parent;
if (parent == 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

if (parent.Fields["TeaserAbstract"] != null)
{
Sitecore.Data.Fields.Field abstractField = parent.Fields["TeaserAbstract"];
if (abstractField.ContainsStandardValue)
output.Write("{0}&#58;&#160;{1}", abstractField.Name + " field", "<b>Contains Standard Value</b><br/>");
else
output.Write("{0}&#58;&#160;{1}", abstractField.Name + " field", "<b>Contains Explicit Value</b><br/>");
}

if (parent.Fields["TeaserLink"] != null)
{
Sitecore.Data.Fields.Field linkField = parent.Fields["TeaserLink"];
if (linkField.ContainsStandardValue)
output.Write("{0}&#58;&#160;{1}", linkField.Name + " field", "<b>Contains Standard Value</b><br/>");
else
output.Write("{0}&#58;&#160;{1}", linkField.Name + " field", "<b>Contains Explicit Value</b>");
}

output.RenderEndTag(); //Div


Check Field Values C# Web Control Output

Abstract field: Contains Explicit Value
TeaserLink field: Contains Explicit Value

Notes

  • This snippet uses a fields collection to reference the fields
  • Returns null if the field does not exist or the field value is null
  • Does not generate inline editing controls for the Page Editor
  • Useful for field types other than simple field types