Testing for Nulls

Testing for Nulls

Controls featured in this example:

  • Sitecore Web Control

Classes featured in this example:

  • Sitecore.Collections.FieldCollection
  • Sitecore.Data.Fields.Field
  • Sitecore.Data.Items.Item
  • Sitecore.Diagnostics.Assert

Testing For Nulls C# Web Control Code

Sitecore.Data.Items.Item item = this.GetItem();
Sitecore.Diagnostics.Assert.IsNotNull(item, "Item cannot be found");

item.Fields.ReadAll();         
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);
//using : for colon and   for space
output.Write("{0}: {1}", "Item", item.DisplayName);
output.RenderEndTag(); //P

output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.P);
output.Write("{0}", "Displaying First Ten Null or Empty Fields");
output.RenderEndTag(); //P

int i = 0;
foreach (Sitecore.Data.Fields.Field field in fields)
{
    if (field == null)
    {
        i++;
        output.Write("{0}&#58;&#160;{1}", field.DisplayName, "<b>null field</b><br/>");
    }
    else
    {
        
        string fieldValue = field.Value;
        if (string.IsNullOrEmpty(fieldValue))
        {
i++;
output.Write("{0}&#58;&#160;{1}", field.DisplayName, "<b>null or empty field value</b><br/>");
        }
    }
    //only display first ten null or empty fields
    if (i >= 10) break;
}

output.RenderEndTag(); //Div


Testing For Nulls C# Web Control Output

Displaying First Ten Null or Empty Fields

Archive Version date: null or empty field value
Help link: null or empty field value
Archive date: null or empty field value
Ribbon: null or empty field value
Reminder text: null or empty field value
Publishing targets: null or empty field value
XSLT Demo: null or empty field value
Workflow: null or empty field value
Read Only: null or empty field value
Short description: null or empty field value