Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Mads Justesen 9 posts 40 karma points
    Dec 13, 2021 @ 08:49
    Mads Justesen
    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.

    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;
    
    }
    
  • Amalie Wowern 144 posts 273 karma points MVP c-trib
    Dec 13, 2021 @ 09:33
    Amalie Wowern
    100

    Try this

       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);
    
                }
    
  • Mads Justesen 9 posts 40 karma points
    Dec 13, 2021 @ 09:35
    Mads Justesen
    1

    That did it!

    Thanks a lot for the aid.

Please Sign in or register to post replies

Write your reply to:

Draft