Copied to clipboard

Flag this post as spam?

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


  • Claushingebjerg 936 posts 2571 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 4499 posts 18254 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 465 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 936 posts 2571 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 489 karma points c-trib
    Nov 07, 2014 @ 14:14
    Gerty Engrie
    101

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

Please Sign in or register to post replies

Write your reply to:

Draft