Copied to clipboard

Flag this post as spam?

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


  • Sebastian Dammark 581 posts 1405 karma points
    Sep 02, 2015 @ 10:15
    Sebastian Dammark
    0

    Finding first descendant with a specific template id ?

    Hi Folks

    How to get the first descendant where templateid = 123 from currentpage ?

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Sep 02, 2015 @ 10:25
    Dennis Aaen
    0

    Hi Sebsatian,

    I think that last comment in this thread https://our.umbraco.org/forum/umbraco-7/using-umbraco-7/52709-Filter-nodes-by-template-alias-in-Umbraco-7 could be interesting for you.

    Hope this helps,

    /Dennis

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Sep 02, 2015 @ 10:35
    Dennis Aaen
    0

    Hi Sebstian

    I think you could use something like this:

    @foreach(var item in CurrentPage.Descendants().Where(Template == "1054").FirstOrDefault()){
        @item.Name
    }
    

    Hope this helps,

    /Dennis

  • Sebastian Dammark 581 posts 1405 karma points
    Sep 02, 2015 @ 13:52
    Sebastian Dammark
    0

    Hej Dennis

    Er der grund til at loope den igennem hvis man bare gør sådan her:

    CurrentPage.Descendants().Where(Template == "1054").First();
    
  • Sebastian Dammark 581 posts 1405 karma points
    Sep 02, 2015 @ 11:24
    Sebastian Dammark
    0

    I was hoping to do something like this:

    var rootPage = CurrentPage.AncestorOrSelf(1);
    var searchPage = rootPage.Children.Where(x => x.TemplateId == 1235).First();
    

    But that just vommits at me with an error looking like this:

    "Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type"

    If I leave out the WHERE part it works fine except that I get the wrong node.

  • Marc Goodson 2148 posts 14353 karma points MVP 8x c-trib
    Sep 02, 2015 @ 13:30
    Marc Goodson
    0

    Hi Sebastian

    I think that's because CurrentPage is dynamic... try:

        var rootPage = Model.Content.AncestorOrSelf(1);
    var searchPage = rootPage.Children.Where(x => x.TemplateId == 1235).FirstOrDefault();   
    
  • Douglas Robar 3570 posts 4711 karma points MVP ∞ admin c-trib
    Sep 02, 2015 @ 14:00
    Douglas Robar
    101

    Hi, Sebastian,

    I take it the search page isn't of a specific document type? That would be easy with something like:

    var searchPage = CurrentPage.Site().FirstChild("ezSearch");
    

    assuming you're using a new-ish version of Umbraco and have .Site() and .FirstChild(). If not, the old-fashioned, long-hand way would be:

    var searchPage = CurrentPage.AncestorOrSelf(1).ezSearches.First();
    

    Note that we're using the pluralized form of the document type alias, which I'm taking a wild guess is 'ezSearch' but if not then change that accordingly.

    If you really must use templates as the only reliable way of finding the appropriate page then you're in good shape except you can't mix LINQ expressions with the simple .Where() syntax provided when using CurrentPage. If you want to use your lambda expressions you'd need to use strongly-typed razor instead of CurrentPage.

    So, assuming you want to use CurrentPage and check for a template it would be:

    var rootPage = CurrentPage.Site();  //or .AncestorOrSelf(1)
    var searchPage = rootPage.Children.Where("TemplateId == 1235").First();
    

    Notice that the .Where() clause used for CurrentPage is a string.

    Last thing to mention... in v7 you won't see the Id number of the template listed in the back office as you used to do in earlier versions. To find the Id number, go to the Templates section of the tree and hover your mouse over the icon to the left of the name of the template you're interested in. You'll get a flyout tooltip that shows the Id number.

    Clearer than mud I hope. Shout if not, my friend!

    cheers,
    doug.

  • Sebastian Dammark 581 posts 1405 karma points
    Sep 03, 2015 @ 09:36
    Sebastian Dammark
    1

    Hi Doug

    Correct, the search page is a general page but with another template. So your last example worked like a charm.

    Thanx a lot :)

    // Sebastian

Please Sign in or register to post replies

Write your reply to:

Draft