Copied to clipboard

Flag this post as spam?

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


  • Claushingebjerg 939 posts 2574 karma points
    Nov 07, 2014 @ 13:54
    Claushingebjerg
    0

    Getting name of nearest ancestor of specific document type

    @CurrentPage.AncestorsOrSelf("Sektion").Last().Name

    Gets me the name of the nearest page of the Doc Type "Sektion".
    But if no ancestor exists of this doc type, i get an error.

    How do i check if an ancestor of the doc type "Sektion" actually exists? 

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Nov 07, 2014 @ 14:02
    Dennis Aaen
    0

    Hi Cluas,

    Maybe you could do something like this:

    if(CurrentPage.AncestorsOrSelf("Sektion")!= null){ 
        @CurrentPage.AncestorsOrSelf("Sektion").Last().Name
    }

    Hope this helps,

    /Dennis

  • Maff 141 posts 466 karma points
    Nov 07, 2014 @ 14:02
    Maff
    0

    Hi,

    You need to check that the ancestor is not null before attempting to get any properties from it.

    So try something like:

    var sektion = @CurrentPage.AncestorsOrSelf("Sektion").Last();

    var sektionName = (sektion != null) ? sektion.Name : string.Empty;

    Cheers,

    Maff

  • Claushingebjerg 939 posts 2574 karma points
    Nov 07, 2014 @ 14:08
    Claushingebjerg
    0

    Using either of your suggestions i get:

     

    Sequence contains no elements

    Exception Details: System.InvalidOperationException: Sequence contains no elements 

  • Gerty Engrie 130 posts 490 karma points c-trib
    Nov 07, 2014 @ 14:14
    Gerty Engrie
    101

    Try using CurrentPage.AncestorsOrSelf("Sektion").Count() > 0 :-)

  • 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