I have created child nodes under a parent node "News" and there is "news date" field in each news, which is picked up by date picker when creating a News item.
In content section of Umbraco, I want to display the node items (news items) descending with the respect to the "news date", How can I do that so umbraco content section displays news items descending respective to the "news date"?
By default, umbraco appends the node as the last node for a certain parent node. But, you could write an event handler that will do the sorting for you (automated) or do the sort manually (via context menu).
If you're going the automated route, then write an event handler that fires on document save or document publish and sort the nodes based on the "news date" property of the nodes.
In your event handler, get all nodes of the same level (having the same parent) and change the sortOrder field in the database for each of the nodes using some sql statements.
Keep in mind that doing on-new/ on-save/ etc sorting is very database intensive. 4.1 makes it a bit easier to 'fake' the sort order in the tree, I'm speaking about this in a few weeks and after that I'll put it up on my blog (sorry, no early sneak peaks :P)
Please review this code. The sorted list of nodes will be in the list staffnewprjnodes.Please tell me how to "updatesortorder" of the parent node with this sorted list?
auto sort nodes by nodedate
Hi
I have created child nodes under a parent node "News" and there is "news date" field in each news, which is picked up by date picker when creating a News item.
In content section of Umbraco, I want to display the node items (news items) descending with the respect to the "news date", How can I do that so umbraco content section displays news items descending respective to the "news date"?
Regards
Nauman
By default, umbraco appends the node as the last node for a certain parent node. But, you could write an event handler that will do the sorting for you (automated) or do the sort manually (via context menu).
If you're going the automated route, then write an event handler that fires on document save or document publish and sort the nodes based on the "news date" property of the nodes.
In your event handler, get all nodes of the same level (having the same parent) and change the sortOrder field in the database for each of the nodes using some sql statements.
Hope this helps.
Regards,
/Dirk
Thanks Dirk, i will give it a try and come back to you, if i need some further help.
Cheers
Nauman
Can find some nice wiki posts on event handling starting here.
/Dirk
Hi Nauman
I'm trying to do the same as you - did you make it work, and if so, would you be so kind and share a codesnippet with me?
Thanks a bunch :)
/Mathias
Take a look at my wiki article, it should help
http://our.umbraco.org/wiki/reference/api-cheatsheet/using-applicationbase-to-register-events/event-examples/change-default-newitems-sorting
Petr
Petr,
Thanks! Can it be also be used to sort by dates from a "news date" field set by datepicker (as described in the first post)?
/Mathias
Keep in mind that doing on-new/ on-save/ etc sorting is very database intensive. 4.1 makes it a bit easier to 'fake' the sort order in the tree, I'm speaking about this in a few weeks and after that I'll put it up on my blog (sorry, no early sneak peaks :P)
Maybe a little late, and I'm not 100% on the code, but wanting to do the same, I came up with the following:
Matt,
Thanks, its working great!
Hey Mathias,
If this solved your problem, maybe you could mark my post as the answer to help others out.
I did make one minor change. At the end i now do a check so that I only resort if the sort order has changed.
When I have a spare moment i'll role it up into a package.
Many thanks
Matt
Hi Matt
Would be happy to mark your aswar as a solution, but can't because I didn't start the thread :-(
Good idea with the check in the end, will try to implement that also.
/Mathias
Ahh, my bad, thought it was your question.
Ahh well, glad I could help none the less.
Matt
Hey Mathias,
I've put toegther a package if you'd like to give it a try:
http://our.umbraco.org/projects/document-sorter
Let me know how you get on
Matt
PS It's my first package so please be gentle
Hi Matt,
Great package, with very good functionality and config options!
For my inspiration and learning, would you mind provide me the final source code?
/Mathias
Hi Mathias,
I've added the source code to the project page.
Many thanks
Matt
Please help me how to alphabeticaly sort media nodes in Umbraco 6. Here is the code snippet showing the work I have done so far:-
void MediaServiceSaved(IMediaService sender, SaveEventArgs e)
{
try
{
foreach (var mediaitem in e.SavedEntities)
{
var parentnode= mediaitem.Parent();
var parentid = parentnode.Id;
var childnodes = parentnode.Children();
var staffprjnodes = new List< TestNode >();
var staffnewprjnodes = new List< TestNode >();
foreach (var childnode in childnodes)
{
int childid = childnode.Id;
string name = childnode.Properties.Where(property => property.Alias == "Name").ToString();
if (!string.IsNullOrEmpty(name))
{
staffprjnodes.Add(
new StaffPrjNode
{
Id = childid,
Name = name
});
}
}
var sortorder = "";
if (staffprjnodes.Count > 1)
{
var sortorder = "";
staffnewprjnodes= staffprjnodes.OrderBy(x => x.Name).ToList();
}}
}}
catch (Exception)
{
throw;
} } }
public class TestNode
{
public int Id { get; set; }
public string Name { get; set; }
}
Please review this code. The sorted list of nodes will be in the list staffnewprjnodes.Please tell me how to "updatesortorder" of the parent node with this sorted list?
Guys...please can anyone suggest ...am I on the right track or not?
is working on a reply...