I have a blog where I would like to list all blogposts in the order they were created (CreateDate), but some posts needs to be backdated, so I have created af Date picker field (datePublished) for that.
But how do I sort/order the items with Razor. I have tried the following without success:
.OrderBy("CreateDate, datePublished") Not working
.OrderBy("CreateDate, datePublished desc") Not working
.OrderBy("CreateDate desc").OrderBy("datePublished desc") Close but no cigar. If datePublished is filled out, the item will always appear over the other items without datePublished set.
Maybe there is an even greater solution to this. Would be great if I was allowed to change the CreateDate field on the Properties tab. But the field is locked.
I prefere to use Strong type like Model.Content instead of dynamic "CurrentPage"
For strong type object you will get the intellisense which make your life easier. below is my code.
var blogPosts = Model.Content.Descendants("BlogPost").Where(x=>x.GetPropertyValue<bool>("Visible")).OrderBy(x=>x.CreateDate).ThenByDescending(x=>x.GetPropertyValue<DateTime>("metaPublishedOn"));
Thanks for your help!!
I have tried your solution Yasir, but I didn't get the result I was looking for. :/
I think the problem is that metaPublishedOn can be empty and then the CreateDate should be used instead. If metaPublishedOn is filled out this should overrule the CreateDate.
The result I got with your solution was the same thing I experienced with my own, if the metaPublishedOn was set the item would get listed first - no matter the date.
Alex I don't know if your solution will help with this problem!?
I did get the job done with the following code (feel free to comment on the code itself - still learning to code Razor properly):
var descendants = CurrentPage.Descendants("BlogPost").Where("Visible");
List<dynamic> items = new List<dynamic>();
foreach (var descendant in descendants){
var publishedOn = (descendant.HasValue("metaPublishedOn")) ? descendant.metaPublishedOn : descendant.CreateDate;
var item = new
{
publishedOn = publishedOn,
id = descendant.Id
};
items.Add(item);
}
List<dynamic> sortedItems = items.OrderByDescending(x=>x.publishedOn).ToList();
This is a good post and pointed me in the right direction, I'll reference my Umbraco 8 solution where I had to combine two lists and sort, similar to above.
var list1 = list; //Model.Children
var list2 = list; //Model.Root().Children.Where(x => x.Id == INT)
Combine Lists
var combined = list1.Concat(list2).ToList();
sort lists as per above example updated to umbraco 8 (i am using custom publicationDate field (metaPublishedOn used above)
var ordered = combined.OrderByDescending(x => x.HasValue("publicationDate") ? x.Value<DateTime>("publicationDate") : x.CreateDate);
OrderBy on multiple date fields (Razor)
I have a blog where I would like to list all blogposts in the order they were created (CreateDate), but some posts needs to be backdated, so I have created af Date picker field (datePublished) for that.
But how do I sort/order the items with Razor. I have tried the following without success:
.OrderBy("CreateDate, datePublished")
Not working
.OrderBy("CreateDate, datePublished desc")
Not working
.OrderBy("CreateDate desc").OrderBy("datePublished desc")
Close but no cigar. If datePublished is filled out, the item will always appear over the other items without datePublished set.
Maybe there is an even greater solution to this. Would be great if I was allowed to change the CreateDate field on the Properties tab. But the field is locked.
Hi Mathias,
Can you share code of your view or macro?
Is it razor?
We will find solution together.
Thanks,
Alex
This is what I have:
I prefere to use Strong type like Model.Content instead of dynamic "CurrentPage"
For strong type object you will get the intellisense which make your life easier. below is my code.
Hope it will help :)
Yasir
Hi Guys,
Yasir, thanks for suggestion, I would change it little bit:
Thanks,
Alex
Thanks for your help!! I have tried your solution Yasir, but I didn't get the result I was looking for. :/
I think the problem is that metaPublishedOn can be empty and then the CreateDate should be used instead. If metaPublishedOn is filled out this should overrule the CreateDate. The result I got with your solution was the same thing I experienced with my own, if the metaPublishedOn was set the item would get listed first - no matter the date. Alex I don't know if your solution will help with this problem!?
I did get the job done with the following code (feel free to comment on the code itself - still learning to code Razor properly):
Hi Mathias,
You are welcome, if I understood your code right, than you can try this code:
Hope it will help you.
Be aware with dynamics and .Descendants - dangerous.
Thanks,
Alex
Spot on - Alex! That worked perfectly.
Just curious - why and when to use AssignedContentItem over Model.Content?
This is a good post and pointed me in the right direction, I'll reference my Umbraco 8 solution where I had to combine two lists and sort, similar to above.
Combine Lists
sort lists as per above example updated to umbraco 8 (i am using custom publicationDate field (metaPublishedOn used above)
is working on a reply...