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 88 posts 362 karma points
    Apr 17, 2023 @ 18:44
    Yaco Zaragoza
    0

    Using the query builder

    I am using the Query builder to generate a list of all the News and Announcements on my site but I can only filter by Created Date, Id, last Updated and/or Name.

    What I am trying to do is filter by one of the properties on the page called "newsType" which is a [Dropdown]NewsType with the values of "News" and "Announcements"

    This is the code I have so far but not sure how to update it so I can get the data that I want.

    @using Umbraco.Cms.Web.Common.PublishedModels;
    @inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage<ContentModels.NewsAndAnnouncementsPage>
    @using ContentModels = Umbraco.Cms.Web.Common.PublishedModels;
    @{
        Layout = "Master.cshtml";
    }
    
    
        <h2>News</h2>
        @{
        var newsSelection = Umbraco.Content(Guid.Parse("4b3fca0c-ecf2-496a-a30d-6168c30a8df3"))
        .ChildrenOfType("newsAndAnnouncementsItemsPage")
        .Where(x => x.IsVisible())
        .Where(x => x.Name.Contains("News"))
        .OrderByDescending(x => x.UpdateDate);
        }
        <ul>
        @foreach (var news in newsSelection)
        {
            <li>
                <a href="@news.Url()">@news.Name()</a>
            </li>
        }
        </ul>
    
        <h2>Announcement</h2>
        @{
            var announcementSelection = Umbraco.Content(Guid.Parse("4b3fca0c-ecf2-496a-a30d-6168c30a8df3"))
            .ChildrenOfType("newsAndAnnouncementsItemsPage")
            .Where(x => x.IsVisible())
            .Where(x => (x.Name.Contains("News") == false))
            .OrderByDescending(x => x.UpdateDate);
        }
        <ul>
            @foreach (var announcement in announcementSelection)
            {
                <li>
                    <a href="@announcement.Url()">@announcement.Name()</a>
                </li>
            }
        </ul>
    
  • Marc Goodson 2155 posts 14406 karma points MVP 9x c-trib
    Apr 18, 2023 @ 06:09
    Marc Goodson
    0

    Hi Yaco

    There are a couple of ways to do this depending if you prefer to use 'Modelsbuilder' or 'IPublishedContent'

    ChildrenOfType will return an IEnumerable

    (IPublishedContent is a generic interface that can describe all page types - regardless of their definition - and so there is a generic way of accessing properties)

    so you could have:

      @{
        var newsSelection = Umbraco.Content(Guid.Parse("4b3fca0c-ecf2-496a-a30d-6168c30a8df3"))
        .ChildrenOfType("newsAndAnnouncementsItemsPage")
        .Where(x => x.IsVisible() &&  x.Value<string>("newsType") == "News" )
        .OrderByDescending(x => x.UpdateDate);
        }
    

    If you are using 'Modelsbuilder' (where Umbraco generates a set of strongly typed models that matches each Document Type, then it would be more like this:

      @{
        var newsSelection = Umbraco.Content(Guid.Parse("4b3fca0c-ecf2-496a-a30d-6168c30a8df3"))
        .Children<NewsAndAnnouncementsItemsPage>()
        .Where(x => x.IsVisible() &&  x.NewsType == "News" )
        .OrderByDescending(x => x.UpdateDate);
        }
    

    Some people prefer one syntax over the other...

    The other thing is you could have a separate News document Type and a separate Announcements Document Type and these could both share the same NewsAndAnnouncements Composition - and then you could filter by News or Announcements Type, rather than having the dropdown. The editors when they create a page would have the choice to create a News item or an Announcement, and they could have different icons in the tree, and then the dropdown wouldn't be needed?

    Also there is a 'cheatsheet' here for razor syntax, that can be super useful to have printed for reference: https://github.com/umbraco/UmbracoDocs/tree/RazorCheatSheet

    regards

    Marc

  • Yaco Zaragoza 88 posts 362 karma points
    Apr 18, 2023 @ 14:36
    Yaco Zaragoza
    0

    Thank you for the clear explanation Marc.

    I am using the Model Builder settings.

    If I want to show these results on my home page as well, do I need this line?

    @inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage<ContentModels.NewsAndAnnouncementsPage>
    
  • Marc Goodson 2155 posts 14406 karma points MVP 9x c-trib
    Apr 19, 2023 @ 09:05
    Marc Goodson
    0

    Hi Yaco

    The @inherits statement at the top of your template tells Umbraco what type the underlying 'Model' should be for the page.

    When a request comes in /about-us/meet-the-team

    Umbraco works out which content item this maps to, and which Document Type is responsible for that content, and then if Modelsbuilder is turned on, it creates a new Model of that type and sends it to the Template View.

    (you can customise all this - but out of the box this is what is happening)

    so in my example, there might be a MeetTheTeam Document Type, so Umbraco would create a MeetTheTeam content model and I'd need

    @inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage<ContentModels.MeetTheTeam>
    

    at the top, to tell the template view that's what type the 'Model' is.

    So for your homepage template you'll probably need something like

    @inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage<ContentModels.HomePage>
    

    (depending on if you see HomePage/Homepage as one or two words)

    In terms of your News Selection query, this should still work ok in your homepage template, because you are getting the 'News section' based on it's unique guid key:

    var newsSelection = Umbraco.Content(Guid.Parse("4b3fca0c-ecf2-496a-a30d-6168c30a8df3"))
    

    and so that will be the same whichever template it is run in.

    regards

    Marc

Please Sign in or register to post replies

Write your reply to:

Draft