Extension method to umbraco Node: GetPropertyValue
Speeding up development is always nice (following DRY philosophy), therefore you can use this Extension (.net 3.5) to get property values without the need for checking if the property is null before getting the actual value. See the sample below.
It also shows how easy it is to Extend the umbraco Node.
[code]//The sample
// Remember to include the using to the namespace where you Extensions is located
using CPalm.Umbraco;
namespace CPalm.masterpages
{
public partial class Test : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
// The problem.
// Can possible give null reference System.NullReferenceException
string str3 = Node.GetCurrent().GetProperty("propertyMaybyExist").Value;
// safe way without the Extension
Property property = Node.GetCurrent().GetProperty("propertyMaybyExist");
string str1 = (property == null) ? string.Empty : property.Value;
// With the extension.
// Will never give a System.NullReferenceException
string str2 = Node.GetCurrent().GetPropertyValue("propertyMaybyExist")
}
}
}[/code]
I created a similar method for .net 2.0 which is designed to work with databinding webcontrols. Here is a sample:
I created a repeater which shows Media items.
[code]
Media media = new Media(Convert.ToInt32(mediaAfbeeldingen.Value));
if (media != null)
{
RptAfbeeldingen.DataSource = media.Children;
RptAfbeeldingen.DataBind();
}
[/code]
In the repeater I call the GetProperty or GetCmsProperty method.
[code]
[/code]
Here are the methods which I placed in a base class.
[code]
public string GetProperty(object item, object propertyAlias)
{
if (item != null && item is Node)
{
Property property = ((Node)item).GetProperty(propertyAlias.ToString());
if (property != null)
{
return property.Value;
}
}
return string.Empty;
}
public string GetCmsProperty(object item, object propertyAlias)
{
if (item != null && item is Content)
{
umbraco.cms.businesslogic.property.Property property = ((Content)item).getProperty(propertyAlias.ToString());
if (property != null)
{
return property.Value.ToString();
}
}
return string.Empty;
}
[quote=slace]that way you'll get the actual type of the node property too :)[/quote]
Nice one! - you forgot the return type though.
Here is the full extension
[code]//The extension (Extensions.cs)
namespace CPalm.Umbraco
{
public static class Extensions
{
public static T GetPropertyValue
Extension method to umbraco Node: GetPropertyValue
Speeding up development is always nice (following DRY philosophy), therefore you can use this Extension (.net 3.5) to get property values without the need for checking if the property is null before getting the actual value. See the sample below.
It also shows how easy it is to Extend the umbraco Node.
[code]//The extension (Extensions.cs)
namespace CPalm.Umbraco
{
public static class Extensions
{
static string GetPropertyValue(this Node node, string alias)
{
Property property = node.GetProperty(alias);
return property == null ? string.Empty : property.Value;
}
}
}[/code]
[code]//The sample
// Remember to include the using to the namespace where you Extensions is located
using CPalm.Umbraco;
namespace CPalm.masterpages
{
public partial class Test : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
// The problem.
// Can possible give null reference System.NullReferenceException
string str3 = Node.GetCurrent().GetProperty("propertyMaybyExist").Value;
// safe way without the Extension
Property property = Node.GetCurrent().GetProperty("propertyMaybyExist");
string str1 = (property == null) ? string.Empty : property.Value;
// With the extension.
// Will never give a System.NullReferenceException
string str2 = Node.GetCurrent().GetPropertyValue("propertyMaybyExist")
}
}
}[/code]
Enjoy :-)
Nice Christian, my initial thoughts is to ask Niels, Per, Tim to get this into the core to replace "GetProperty"
Warren :)
I published a super-sized version of this (well, a method doing this for Umbraco members) in this blog post: http://www.aaron-powell.com/blog/august-2008/extending-umbraco-members.aspx
It also shipped in the Umbraco Interaction Layer (but was document based): http://uil.codeplex.com
Here's a node version:
[code]
public static GetPropertyValue
I created a similar method for .net 2.0 which is designed to work with databinding webcontrols. Here is a sample:
I created a repeater which shows Media items.
[code]
Media media = new Media(Convert.ToInt32(mediaAfbeeldingen.Value));
if (media != null)
{
RptAfbeeldingen.DataSource = media.Children;
RptAfbeeldingen.DataBind();
}
[/code]
In the repeater I call the GetProperty or GetCmsProperty method.
[code]
[/code]
Here are the methods which I placed in a base class.
[code]
public string GetProperty(object item, object propertyAlias)
{
if (item != null && item is Node)
{
Property property = ((Node)item).GetProperty(propertyAlias.ToString());
if (property != null)
{
return property.Value;
}
}
return string.Empty;
}
public string GetCmsProperty(object item, object propertyAlias)
{
if (item != null && item is Content)
{
umbraco.cms.businesslogic.property.Property property = ((Content)item).getProperty(propertyAlias.ToString());
if (property != null)
{
return property.Value.ToString();
}
}
return string.Empty;
}
[/code]
This way you can call a property in a webcontrol.
[quote=slace]that way you'll get the actual type of the node property too :)[/quote]
Nice one! - you forgot the return type though.
Here is the full extension
[code]//The extension (Extensions.cs)
namespace CPalm.Umbraco
{
public static class Extensions
{
public static T GetPropertyValue
Hi, here's another Umbraco helper class of static methods aimed at .net 2.0.
HTH,
Hendy
is working on a reply...