Copied to clipboard

Flag this post as spam?

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


  • hetaurhet 245 posts 267 karma points
    Apr 21, 2012 @ 11:55
    hetaurhet
    0

    to get root node in ascx

    hello...

    I have .net UC in which I want to access rootnode (last grand parent) of node(this node will have the .ascx macro).

    In, umbraco panel, I have 4 websites. So, how to get root node in .ascx? which dll to refrence?

  • Gregg Duncan 48 posts 70 karma points
    May 04, 2012 @ 23:24
    Gregg Duncan
    0

    hello hetuarhet,

    Hopefully this will help you or someone else looking for the same type of thing. We are also running a multi-site installation of umbraco. I found myself needing this type of information frequently while I was building our sites. So, I created this static umbracoHelper class with a list of extension methods. I've found them very helpful so I'm happy to be able to share them.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using umbraco.NodeFactory;


    namespace [Insert your namespace here]
    {
    public static class umbracoHelpers
    {
    /// <summary>
    /// Does a recursive check up the node tree to find a property with the specified alias.
    /// If found it returns its value.
    /// If not found it returns an empty string.
    /// </summary>
    /// <param name="node"></param>
    /// <param name="alias"></param>
    /// <returns></returns>
    public static string getNodeValueRecursively(this Node node, string alias)
    {
    if (node == null) return string.Empty;
    string value = node.checkNodeForValue(alias);
    while (string.IsNullOrEmpty(value) && node.Parent != null)
    {
    node = (Node)node.Parent;
    value = node.checkNodeForValue(alias);
    }
    return value;
    }

    /// <summary>
    /// Checks the current node for a property with the
    /// specified alias and returns that properties value if found.
    /// Returns an empty string if the alias is not found.
    /// </summary>
    /// <param name="node">current node</param>
    /// <param name="alias">Properties Alias for which to search</param>
    /// <returns></returns>
    public static string checkNodeForValue(this Node node, String alias)
    {
    string code = string.Empty;
    if (node == null) return code;
    foreach (Property prop in node.Properties)
    {
    if (prop.Alias != alias)
    continue;
    code = prop.Value;
    break;
    }
    return code;
    }

    /// <summary>
    /// Finds the website root node by using the getAncestorNodeByLevel passing level 1 as the parameter.
    /// </summary>
    /// <param name="current">current node</param>
    /// <returns>root node</returns>
    public static Node getRootNode(this Node current)
    {
    return current.getAncestorNodeByLevel(1);
    }

    /// <summary>
    /// Returns the node that is the ancestor of the node passed at the indicated level.
    /// Level 1 = the root node for this website.
    /// Level 2 = the Main menu level node
    /// Level 3 = the Sub menu level node
    /// If the level requested does not exist null is returned
    /// </summary>
    /// <param name="node">the node to search</param>
    /// <param name="level">the node level for which to search</param>
    /// <returns></returns>
    public static Node getAncestorNodeByLevel(this Node node, int level)
    {
    if (node == null || level <= 0) return null;
    List<String> nodelist = node.Path.Split(',').ToList();
    if (nodelist == null || nodelist.Count <= level) return null;
    int nodeid = Convert.ToInt32(nodelist[level]);
    return new Node(nodeid);
    }

    /// <summary>
    /// Gets a count of all decendants of the node passed.
    /// </summary>
    /// <param name="node">Node on which to run the count</param>
    /// <returns>Count of decendant nodes.</returns>
    public static int getDescendantNodeCount(this Node node)
    {
    int count = node.Children.Count;
    foreach (Node child in node.Children)
    {
    count += child.getDescendantNodeCount();
    }
    return count;
    }

    /// <summary>
    /// Get decendant node count for sitemap
    /// </summary>
    /// <param name="node">Node on which to perform the count</param>
    /// <returns>Count of decendant where the umbracoSitemapHide is set to false</returns>
    public static int getSitemapNodeCount(this Node node, List<string> nodeTypeAliasesToAutoHideInSitemap)
    {
    int count = 0;
    Property sitemapHide = (Property)node.GetProperty("umbracoSitemapHide");
    if ((sitemapHide != null && sitemapHide.Value == "1") || nodeTypeAliasesToAutoHideInSitemap.Contains(node.NodeTypeAlias.ToLower())) return count;
    foreach (Node child in node.Children)
    {
    Property thisSitemapHide = (Property)child.GetProperty("umbracoSitemapHide");
    if ((thisSitemapHide != null && thisSitemapHide.Value == "1") || nodeTypeAliasesToAutoHideInSitemap.Contains(child.NodeTypeAlias.ToLower())) continue;
    count += child.getSitemapNodeCount(nodeTypeAliasesToAutoHideInSitemap) + 1;
    }
    return count;
    }
    }
    }

     

  • Travis Nelson 7 posts 27 karma points
    Oct 30, 2012 @ 22:19
    Travis Nelson
    0

    Thank you for this Gregg. I'm going to implement this as a starting point for an UmbracoHelper library in my site. I needed one and just so happened yours did a couple of things I needed it to. :)

Please Sign in or register to post replies

Write your reply to:

Draft