Copied to clipboard

Flag this post as spam?

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


  • maanehunden 61 posts 105 karma points
    Aug 15, 2013 @ 10:18
    maanehunden
    0

    Finding Parent Node By Alias using Umbraco 6 ?

    Hi All

    What is your opinion on the following code, i have to find parent of a specific type. I Know that "Document" is deprecated, so what is the best way of doing this with "Content".

    thanks.. Maanehunden

        public static Document FindParentNodeByAlias(this Document current, string alias)
        {
            Document found = current;
            while (found.Id != -1)
            {
                found = new Document(found.ParentId);
                if (found.ContentType.Alias == alias)
                {
                    break;
                }
            }
    
            return found;
        }
    
  • João Ferreira 19 posts 40 karma points
    Aug 15, 2013 @ 10:55
    João Ferreira
    0

    Hi, I usually use this method to find it:

    public static DynamicNode GetParentByNodeType(DynamicNode node, string parentNodeType)
        {
            DynamicNode parent = node;
            for (int i = 0; i < node.Level; i++)
            {
                if (parent.NodeTypeAlias == parentNodeType)
                {
                    break;
                }
     
                parent = parent.Parent;
            }
     
            return parent;
        }
     

    I hope that helps. Also about getting a parent I have this ones in my helper:

    public static Document GetParentByLevel(Document node, int level)
        {
            CMSNode p = new CMSNode(node.Id);
            for (int i = 0; i < node.Level; i++)
            {
                if (p.Level == level)
                {
                    break;
                }
                p = p.Parent;
            }
     
            return new Document(p.Id);
        }
     
        public static List<string> GetAllParentToLevel(DynamicNode node, int level)
        {
            DynamicNode parent = node;
            List<string> parents = new List<string>();
            for (int i = 0; i < node.Level; i++)
            {
                if (parent.Level == level)
                {
                    break;
                }
                parents.Add(parent.Id.ToString());
                parent = parent.Parent;
            }
     
            return parents;
        }
     
    You can use either Document or DynamicNodes.

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Aug 15, 2013 @ 10:58
    Dave Woestenborghs
    100

    The document api was meant for creating/updating content. If you are using v6 have a look at the contentservice API (http://our.umbraco.org/documentation/reference/Management-v6/Services/ContentService)

    For querying content to use on the frontend you should use the Node API (v4) or TypedContent(V6). As a alternative you could use uQuery.

    You can find some documentation here : http://our.umbraco.org/documentation/Reference/Querying/uQuery/Content/Nodes (v4 and v6)

    http://our.umbraco.org/documentation/reference/Mvc/querying (v6 only)

    So you would be looking for something like this :

    Node homeNode = uQuery.GetCurrentNode().GetAncestorNodes().Where(x => x.NodeTypeAlias == "home").FirstOrDefault();

    This would result in the first ancestor node with node type alias "home"

    Dave

  • maanehunden 61 posts 105 karma points
    Aug 15, 2013 @ 12:01
    maanehunden
    0

    Thanx for your replies Dave and João.

    uQuery is clearly the way (c:

    /Maanehunden

  • andrew shearer 513 posts 663 karma points
    Aug 16, 2013 @ 04:06
    andrew shearer
    0

    uQuery is not the way! uQuery doesn't use the new library that was introduced in umbraco 6. It uses the old libraries from 4.

    @maanehunden - where are you trying to use this method, a view? if so, you should be able to use:

    Model.Content.AncestorOrSelf("yourAliasHere")


  • maanehunden 61 posts 105 karma points
    Aug 16, 2013 @ 08:51
    maanehunden
    0

    Tnx for the reply Andrew.

    I am working on a custom datatype for the backoffice, so i can't use Model.Conten ): the Node is not populated so i am forced to use Document, because it works.

    any other suggestions ?

  • andrew shearer 513 posts 663 karma points
    Aug 16, 2013 @ 09:07
    andrew shearer
    0

    Document is now known in the umb6 API as "Content":

    http://our.umbraco.org/documentation/reference/Management-v6/Models/Content

     although, that page says not to use it for "complex queries" as it might not be fast enough, so I'm not sure what the Umbraco team suggest instead :(

     

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies