Retrieving Umbraco property values
A quick code snippet that can be particularly useful when implementing a models tier in an Umbraco solution. This code I usually implement in a base class, giving inheriting classes access to the method. The below method uses generics to retrieve a property from a node by it's alias:
using umbraco;
using umbraco.interfaces;
using umbraco.NodeFactory;
internal Node currentNode = new Node();
internal T GetProperty<t>(String propertyAlias)
{
T result = default(T);
try
{
if (currentNode.HasProperty(propertyAlias))
{
IProperty property = currentNode.GetProperty(propertyAlias);
result = (T)Convert.ChangeType(property.Value, typeof(T));
}
}
catch { }
return result;
}