This project is read-only.

Sorting Blogs

Topics: Core, Customizing Orchard, General, Writing modules
Sep 24, 2013 at 1:51 PM
Edited Sep 24, 2013 at 1:57 PM

I was wondering if someone can recommend the best way to order blog list by created date (desc/asc). I have added an "order by" field in the blog, and in Parts.Blogs.BlogPost.List.cshtml tried to order below blogPosts:

IEnumerable<object> blogPosts = Model.ContentItems;

How and where should I make this customization?

Please help. Also, we are in Orchard 1.6.

Sep 24, 2013 at 5:29 PM
Create a query, use it in a projection.
Sep 24, 2013 at 6:27 PM
BertrandLeRoy wrote:
Create a query, use it in a projection.
Thank you Bertrand. The problem is that the requirement is to have a flag for Order on Blog content type. Once the flag is set, the order should dynamically change on the front end, so I would have to figure a way to flip projections/queries. Sorry I'm new to this. Is this the only way I can order? Is there another way besides possibly customizing blog module? I was hoping to customize shape only. Thanks.
Sep 24, 2013 at 6:48 PM
oh, ok. Should be easy to write a custom controller that changes the sorting based on some boolean field.
Sep 24, 2013 at 7:06 PM
Do you have an example of that? I'm confused why that's an easy option. I should be able to get contentitem with a custom field which contains the flag, then sort by created date in Common part, like in linq orderby list. Also, I've looked into the the query in the blog service, and it's fairly complex to overload and customize.
Sep 24, 2013 at 7:26 PM
The problem is that the ordering filter is not configurable in this way, so I'm trying to find you a workaround. It seems like you are looking for an admin-driven solution. Then maybe you need to consider the sort order as that flag. It won't be on the blog editor itself, but it will work the same way, no?
Sep 24, 2013 at 8:54 PM
A projection would definitely work for this, although if you're looking for an easy way for admin users to modify the sort order, then it might be a little intimidating for non-technical users to go in and modify the underlying query, even though Orchard provides a very nice UI to do so.

If you would like to add a field to the blog type for this, then there's always the brute-force way... ;) You may be able to overload BlogPostService.GetArchives() to add a sort order parameter (or better yet, a more extensible filter object). You could then change that parameter based on the "order by" value stored for the blog. Just thinking of an approach that would be applicable across the board, to the archive widget, list page, etc... And to me that implies something in the service layer.
Sep 25, 2013 at 12:16 AM
You have to choose: either you do it from the admin only and it's clumsy to manage, or you write code to make it easier for your user.