I'm currently trying to sort the order of the content pages, by created date, whenever the Editor saves the document.
My current attempt is to create a composer which is listining on Saving and then Sort the order. I have a feeling that I've completly misunderstood something about how this function and would love it if anyone could give me a hand with what I'm doing.
This is my current code.
The project is running on Umbraco version 8.16.0 if it has any relevance.
class SortWhenCreateOrUpdateComposer : ComponentComposer<SortWhenCreateOrUpdateComponent>
{ }
public class SortWhenCreateOrUpdateComponent : IComponent
{
public void Initialize() => ContentService.Saving += ContentService_Saving;
private void ContentService_Saving(IContentService sender, ContentSavingEventArgs e)
{
foreach (IContent node in e.SavedEntities)
{
if (node.HasIdentity == false)
{
var helper = Umbraco.Web.Composing.Current.UmbracoHelper;
var parentCheck = helper.Content(node.ParentId);
//var children = helper.Content(node.ParentId).Children;
//if (parentCheck == false) return;
//var parent = sender.GetById(node.ParentId);
//sender.Save(node);
var children = sender.GetPagedChildren(node.ParentId, 0, int.MaxValue, out var count);
var temp = children.OrderBy(c => c.SortOrder).First().SortOrder - 1;
sender.Sort(children, 0, false);
//node.SortOrder = 1;
//sender.Save(node);
}
}
}
public void Terminate() => ContentService.Saving -= ContentService_Saving;
}
if (node.HasIdentity == false)
{
var helper = Umbraco.Web.Composing.Current.UmbracoHelper;
var children = new List<int>();
children.Add(node.Id);
children.AddRange(helper.Content(node.ParentId).Children.OrderByDescending(n => n.CreateDate).Select(child => child.Id));
sender.Sort(children, 0);
}
Sort document types when Editor saves.
Hej there!
I'm currently trying to sort the order of the content pages, by created date, whenever the Editor saves the document.
My current attempt is to create a composer which is listining on Saving and then Sort the order. I have a feeling that I've completly misunderstood something about how this function and would love it if anyone could give me a hand with what I'm doing.
This is my current code.
The project is running on Umbraco version 8.16.0 if it has any relevance.
Try this
That did it!
Thanks a lot for the aid.
is working on a reply...