Copied to clipboard

Flag this post as spam?

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


  • Pasang Tamang 258 posts 458 karma points
    Feb 28, 2019 @ 14:28
    Pasang Tamang
    0

    Get Descendants Node By DocumentType Alias Not Working

    So started to play around with Umbraco 8. Improvement done in Umbraco 8 is really a great. But I feel like I am starting it again from beginning that I start to learn Umbraco 4.x version. I am trying from very simple code with default starter kit that comes with Umbraco. I have below code trying to show list of blogs in home page.

    @inherits Umbraco.Web.Mvc.UmbracoViewPage
    @{
        Layout = null;
        var home=Model.AncestorOrSelf(1);
        var blogs=home.Descendants("blog").FirstOrDefault();
        var product=home.Descendant("product");
    }
    <h1>@blogs.Name</h1>
    @foreach(var blog in home.Descendants("blogpost"))
    {
        <a href="@blog.Url">Title: @blog.Name</a><br/>
    }
    

    Above code is giving me completely different result then I expect. @blogs.Name I want to show node name of the blog node. But that's showing name of the product node. And my loop code shows all the nodes instead of listing only blogs. Can anyone suggest me whats wrong in my code? Or there is different code available for Umbraco 8? As I have seen in razor examples still descendants with document type alias parameter is supporting.

    Thanks

    Pasang

  • Rhys Hamilton 140 posts 942 karma points
    Feb 28, 2019 @ 14:47
    Rhys Hamilton
    100

    The reason all of your nodes are being listed is because your foreach is looping through the home list, instead of blogs.

    Having said that, the following code below should produce what you'd expect to see:

    @{
        // Get the first "Blog" document type that exists within Umbraco
        var blog = Model.Root().Children.Where(x => x.IsDocumentType("blog")).FirstOrDefault();
    
        <h1>@blog.Name</h1>
    
        // Iterates through the list of blog children
        foreach (var item in blog.Children)
        {
            <a href="@item.Url">@item.Name</a><br/>
        }
    }
    
  • Pasang Tamang 258 posts 458 karma points
    Feb 28, 2019 @ 15:43
    Pasang Tamang
    0

    Hi Rhys,

    Thanks for your quick response. It's a great help. It solved my problem. I thought home.Descendants("blog") will still give me the blog node as in U7.

    Once again thank you for your help.

    Thanks Pasang

Please Sign in or register to post replies

Write your reply to:

Draft