Reputation: 311
I have a site with structure like Home -> News -> Year -> Month -> Day -> Article
and need a query to find the id of the Day
folder where the name of the folder matches a specified Year
, Month
and Day
.
For example for 12th May 2003 it would be something like:
@something().where("Year.name = 2003").where("Month.name = May").where("Day.name = 12")
Upvotes: 1
Views: 974
Reputation: 4450
Depending on your preference, you can construct a query using either the dynamic DynamicPublishedContent or the strongly-typed IPublishedContent API.
The query will be different depending on where it's used, so assuming you want to run this query and display the results in the "News" page, you need something like the following:
var year = CurrentPage.Years.Where("Name == @1", "2013").First();
var month = year.Months.Where("Name == @1", "May").First();
var day = month.Days.Where("Name == @1", "12").First();
var dayId = day.Id;
Alternatively, you can try the strongly typed version:
var day = Model.Content
.Descendants("Year").First(y => y.Name == "2013")
.Descendants("Month").First(m => m.Name == "May")
.Descendants("Day").First(d => d.Name == "12").Id;
I've used First()
for brevity but in reality you might want to check if the page exists before getting its descendants. Also, you might want to have a look at the documentation and come up with a query that's better suited to your scenario.
Upvotes: 2