Copied to clipboard

Flag this post as spam?

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


  • Claushingebjerg 937 posts 2572 karma points
    Oct 27, 2014 @ 16:01
    Claushingebjerg
    0

    Getting a property from the nearest ancestor of document type

    Im trying to do som recursivenes. I need to pull a value from a property from the nearest ancestor of two document types. Which ever is nearest... 

    @if (CurrentPage.HasValue("Kontaktpersoner")){ 
        <p>CurrentPage has kontaktperson</p>
    } else if (CurrentPage.AncestorsOrSelf("Sektion").First().HasValue("Kontaktpersoner")){
      <p>Ancestor of doctype Sektion has kontaktperson</p>
    } else if (CurrentPage.AncestorsOrSelf("Afdeling").First().HasValue("Kontaktpersoner"){   <p>Acncestor of doctype Afdeling has kontaktperson</p>
    } else {
      <p>No node has kontaktperson</p>
    }

    But this throws an error if both document types dont exist in the Ancestoror self scope.

    How do i fix this?

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Oct 27, 2014 @ 17:37
    Dennis Aaen
    101

    Hi Claus,

    If you do something like this:

    @{
        if (CurrentPage.HasValue("Kontaktpersoner", true)){
            <p>CurrentPage has kontaktperson</p>
        }
    }

    Then it will look recursively for the property value in ancestors. So if current page doesn´t have the property value then it will go up in the tree, until it found it.

    Hope this helps,

    /Dennis

  • Claushingebjerg 937 posts 2572 karma points
    Oct 28, 2014 @ 08:36
    Claushingebjerg
    0

    Holy c...

    That was clean! and it works like a charm. I just cut my view in less than half.

    Thanks a lot :)

     

Please Sign in or register to post replies

Write your reply to:

Draft