Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Christian Palm 278 posts 273 karma points
    May 28, 2009 @ 10:48
    Christian Palm
    0

    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 :-)

  • Warren Buckley 2106 posts 4836 karma points MVP 7x admin c-trib
    May 28, 2009 @ 11:01
    Warren Buckley
    0

    Nice Christian, my initial thoughts is to ask Niels, Per, Tim to get this into the core to replace "GetProperty"

    Warren :)

  • Aaron Powell 1708 posts 3046 karma points c-trib
    May 28, 2009 @ 15:28
    Aaron Powell
    1

    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

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Jun 09, 2009 @ 14:09
    Jeroen Breuer
    0

    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.

  • Christian Palm 278 posts 273 karma points
    Jun 12, 2009 @ 14:37
    Christian Palm
    0

    [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

  • Hendy Racher 863 posts 3849 karma points MVP 2x admin c-trib
    Aug 26, 2010 @ 15:42
    Hendy Racher
    0

    Hi, here's another Umbraco helper class of static methods aimed at .net 2.0.

    HTH,

    Hendy

Please Sign in or register to post replies

Write your reply to:

Draft