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
    Jun 12, 2009 @ 09:37
    Christian Palm
    1

    Extension method to umbraco Nodes: Find(string childName)

    This extension method makes is possible to find a child node with a specifiq name:

    [code]//The extension (Extensions.cs)
    namespace CPalm.Umbraco
    {
    public static class Extensions
    {
    public static Node Find(this Nodes nodes, string name)
    {
    foreach (Node node in nodes)
    {
    if (node.Name.ToLowerInvariant() == name.ToLowerInvariant())
    return node;
    }
    return null;
    }
    }
    }[/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)
    {
    Node currentNode = Node.GetCurrent();
    // Find the child with the name Config
    Node configNode = currentNode.Children.Find("Config");
    if (configNode == null)
    {
    throw new Exception("Could not find child node");
    }
    }
    }
    }[/code]


    Other extension methods:
    Extension method to umbraco Node: ToXPathNodeIterator http://bit.ly/baHwn
    Extension method to umbraco Node: GetPropertyValue http://bit.ly/CPalmGPV


    Enjoy

  • Daniel Lindstrom 454 posts 271 karma points
    Jun 12, 2009 @ 09:55
    Daniel Lindstrom
    0

    These are all great small extension methods. I think many of us has written code to handle similar tasks over and over and over again in different projects. Making extension methods out of them like this does save dev and testing time, and publishing the code here will benefit the community. What I want to say is: Thanks for sharing!

Please Sign in or register to post replies

Write your reply to:

Draft