Copied to clipboard

Flag this post as spam?

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


  • Yaco Zaragoza 80 posts 334 karma points
    Apr 21, 2023 @ 13:32
    Yaco Zaragoza
    0

    Showing Properties from Item using the query builder

    I am using the "ModelsBuilder" Mode and I am testing the Query Builder to get a list of all my "News" items but it seems like it is only able to get the Name and Url for the items but nothing else..

    This is the code that got generated.

    @{
        var inTheNews = Umbraco.Content(Guid.Parse("257d436e-d180-46af-b37e-585136836c8d"))
        .ChildrenOfType("newsItemPage")
        .Where(x => x.IsVisible())
        .OrderByDescending(x => x.CreateDate);
    }
    <ul>
        @foreach (var item in inTheNews)
        {
            <li>
                <a href="@item.Url()">@item.Name()</a>
            </li>
        }
    </ul>
    

    I would like to be able to get the properties like Title and Short Description

    Also, I was trying to update the query so it would only generated a list of items between Start Date and End

  • Marc Goodson 2141 posts 14344 karma points MVP 8x c-trib
    Apr 22, 2023 @ 16:02
    Marc Goodson
    100

    Hi Yaco

    'ChildrenOfType' returns an IEnumerable of IPublishedContent - where each IPublishedContent item represents your news item in a 'generic way'.

    With IPublishedContent you can access the properties using the .Value helper

    So in your example above you'd be able to write out your title and Short Description by using their alias in the backoffice on the doc type.

    so

    @{
        var inTheNews = Umbraco.Content(Guid.Parse("257d436e-d180-46af-b37e-585136836c8d"))
        .ChildrenOfType("newsItemPage")
        .Where(x => x.IsVisible())
        .OrderByDescending(x => x.CreateDate);
    }
    <ul>
        @foreach (var item in inTheNews)
        {
            <li>
                <a href="@item.Url()">@item.Name()</a> - @item.Value<string>("title") - @item.Value<string>("shortDescription")
            </li>
        }
    </ul>
    

    But if you want to use the 'Modelsbuilder' approach to listing out the children then you would have instead

    @{
        var inTheNews = Umbraco.Content(Guid.Parse("257d436e-d180-46af-b37e-585136836c8d"))
        .Children<NewsItemPage>()
        .Where(x => x.IsVisible())
        .OrderByDescending(x => x.CreateDate);
    }
    <ul>
        @foreach (var item in inTheNews)
        {
            <li>
                <a href="@item.Url()">@item.Name()</a> - @item.Title - @item.ShortDescription
            </li>
        }
    </ul>
    

    Here Umbraco knows the children are explicitly NewsItemPage types and so you can use the strongly typed property access instead of .Value

    You can then use the properties for filtering too eg

    @{
        var inTheNews = Umbraco.Content(Guid.Parse("257d436e-d180-46af-b37e-585136836c8d"))
        .Children<NewsItemPage>()
        .Where(x => x.IsVisible() && x.PublishDateOrWhatever > new DateTime("somedate"))
        .OrderByDescending(x => x.CreateDate);
    }
    

    This cheatsheet is geared for using Modelsbuilder and might give you some hints for working with Children etc

    https://drive.google.com/file/d/171sMUOjjKv-bvlFEDNk7WN0I_UD9g670/view

    regards

    Marc

  • Yaco Zaragoza 80 posts 334 karma points
    Apr 22, 2023 @ 16:50
    Yaco Zaragoza
    0

    Thank you for the very clear explanation and the cheat sheet, I will try to get more familiar with it so I can keep learning and improving with Umbraco!

Please Sign in or register to post replies

Write your reply to:

Draft