Checking if a Sitecore field has a value
2012-07-23 | Sitecore TipsWhen trying to determine if a Sitecore field has a value there are two basic ways.
Sitecore.Context.Item.Fields["fieldName"].HasValue
or
Sitecore.Context.Item.Fields["fieldName"].Value != ""
There are a few major differences.
- HasValue will not include checking a clone's original value, Standard Values or Default Values
- HasValue will return true if the field value is an empty string
- Field.Value will return the clone's original value, Standard Value or Default Values if the field doesn't have a value
- If there is no value on the field, clone's original value, standard values, or default value Field.Value will return an empty string
Therefore, Sitecore.Context.Item.Fields["fieldName"].Value == null
will never evaluate to true!
Beware. Sitecore Bug
By calling Field.HasValue
you will cause the Field.ContainsStandardValues
property to have an invalid value! This has been logged as bug #368493 with Sitecore. For more information see this post.
Comments