Copied to clipboard

Flag this post as spam?

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


  • Brendan Rice 538 posts 1102 karma points
    Jan 21, 2011 @ 17:34
    Brendan Rice
    0

    Access first ancestor node of certain doc type

    I know how to get the first ancestor node of a certain document type in XSLT, how do yoiu achieve the same thing in C# code?

  • Hendy Racher 863 posts 3849 karma points MVP 2x admin c-trib
    Jan 21, 2011 @ 17:53
    Hendy Racher
    2

    Hi Brendan,

    To give you a code example which ancestor are you after as nodes of a certain document type can be scattered though-out the tree ? The uQuery helper library in uComponents may be useful as there are methods to get NodesByType() and GetNodesByXPath(), as well as being able to iterate though the tree with XPath axis type extension methods on the Node and Media objects.

    HTH,

    Hendy

  • Sascha Wolter 615 posts 1101 karma points
    Jan 22, 2011 @ 21:10
    Sascha Wolter
    2

    Hi Brendan,

    +1 for the above. Here is some code you might find useful (out of the top of my head, so there might be some syntax errors...):

    var currentNode = Node.GetCurrent();

    var myDocumentType = "ancestor doc type to look for";

    var level = 2;

    var ancestorId = GetAncestor(currentNode, myDocumentType, level);

     

    private int GetAncestor(Node currentNode, string myDocumentType = "", int level = -1){

      if (currentNode != null) {

        if ((string.IsNullOrEmpty(myDocumentType) || currentNode.NodeTypeAlias == myDocumentType) &&

           (level < 0 || currentNode.Level == level)){

           return currentNode.Id;

         } else if (currentNode.Parent != null) {

          Node parent = new Node(currentNode.Parent.Id);

          return GetAncestor(parent, myDocumentType, level);

        }

    }

    Hope that makes sense and helps,

    Sascha

  • Hendy Racher 863 posts 3849 karma points MVP 2x admin c-trib
    Jan 22, 2011 @ 21:44
    Hendy Racher
    2

    Hi,

    I've not tried either yet, but I wonder if this is the equivalent using Linq and uQuery ?

    Node resultNode = uQuery.GetCurrentNode()
                         .GetAncestorNodes()
                            .Where(node => node.NodeTypeAlias == "myDocumentTypeAlias")
                            .FirstOrDefault();

    Cheers,

    Hendy

     

Please Sign in or register to post replies

Write your reply to:

Draft