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.
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)
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:
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?
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
(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.
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.
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:
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:
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
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?
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
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
(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:
and so that will be the same whichever template it is run in.
regards
Marc
is working on a reply...