Anyone? I'm trying to add a archive by year feature to my site, but I can't believe how difficult it is to achieve such a simple and common design pattern.
Thanks for the reply. That seems like such a lot of code for such a simple function. I decided to remove all sorting params and ask the editor to organise items in the Site tree according to date. I've managed to get the year from the URL, but can't quite figure out how to pass in the variable into the loop?
@{
// Get year from URL
var url = HttpContext.Current.Request.Url;
var year = url.Segments[3].TrimEnd('/');
string[] yearArray = year.Split('-');
var myList = new List<umbraco.MacroEngines.DynamicNode>();
foreach (dynamic page in @Model.AncestorOrSelf(1).DescendantsOrSelf().Where("publishDate.Year == 2013")) {
//foreach (dynamic page in @Model.AncestorOrSelf(1).DescendantsOrSelf()) {
if(page.NodeTypeAlias == "LatestUpdate")
{
myList.Add(page);
}
}
}
Year filtering via URl
I'd like to do two things:
Here is my code:
1) Sorting by CreateDate is not ideal for importing old content as it can't be in the past. So I've added a new filed called publishDate. But
Throws an error. Any idea how to sort by custom date?
2) I want:
To be dynamic, preferably from the last url segment, which may be december-2014, or january-2013
I'd rather not use query strings as the URLs are dynamically generated elsewhere and adding the query string parameter would be hard.
Thanks!
Anyone? I'm trying to add a archive by year feature to my site, but I can't believe how difficult it is to achieve such a simple and common design pattern.
I believe sorting and filtering with custom properties can be achieved by:
Don't forget to add the using statement in the razorscript at the top to use Linq:
As for getting the filter out of the url, you can access the url using
Which is an Uri object: http://msdn.microsoft.com/en-us/library/system.uri%28v=vs.110%29.aspx
Take a look at the class properties, one of them should be able to get and parse the data out of the url.
Thanks for the reply. That seems like such a lot of code for such a simple function. I decided to remove all sorting params and ask the editor to organise items in the Site tree according to date. I've managed to get the year from the URL, but can't quite figure out how to pass in the variable into the loop?
So I need the equivalent of:
Please?
Why not just add the year to the string in the Where method? Like so:
is working on a reply...