Determining if a content item has a specific property available

Since the $sys.item.hasProperty() method will return false if the value of the property is null, then we need to use another method to determine if a property exists within the current item’s context.

Here’s a way that can be used in both a template and a binding:

$sys.item.getDefinition().getDefaultPrimaryType().getAllJSR170Properties().contains('rx:[i]property_name[/i]')

The “rx:” prefix is important here as the names in the returned property name set all have it, so without the prefix, you will always get false.

For example:

$sys.item.getDefinition().getDefaultPrimaryType().getAllJSR170Properties().contains('rx:sys_contentid')

will always return true.

Anotyher example:

$sys.item.getDefinition().getDefaultPrimaryType().getAllJSR170Properties().contains('rx:foo')

will return true only if the content type of the current item has a property defined called “foo”, otherwise, it’ll return false.

Your other option is to call $sys.item.getProperty() and handle the PathNotFoundException.

If you get a property and it is NULL, then the property is defined for that type (but not provided in that item). If you get a PathNotFoundException, then the property is not defined for that content type.

True… but how do you do that in a velocity template or in a binding without causing the template to fail?