0

In Umbraco 6, when you create a new node, it is put at the bottom.
You have to sort it manually if you want it to be on the top.

How can you make new nodes appear on the top by default?

Aximili
  • 28,626
  • 56
  • 157
  • 216

2 Answers2

1

You could create an event handler that changes the sort order of the nodes when the new node is created. See Application startup events & event registration for more details on implementing an handler of your own.

Rough untested example which I am sure you could make more elegant but should point you in the right direction:

public class YourApplicationEventHandlerClassName : ApplicationEventHandler
{
    protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
    {
      ContentService.Created += ContentServiceCreated;
    }

    private void ContentServiceCreated(IContentService sender, NewEventArgs<IContent> e)
    {
      var cs = ApplicationContext.Current.Services.ContentService;
      var content = e.Entity;
      var parentNode = content.Parent();

      content.SortOrder = parentNode.Children().OrderBy(n => n.SortOrder).First().SortOrder - 1;
      cs.Save(content);
    }
}
ProNotion
  • 3,662
  • 3
  • 21
  • 30
  • In addition to the example above you'd better check the parent has child nodes first or you will get a null reference exception. – ProNotion Feb 26 '14 at 08:22
  • Thanks for this. But when will that function get executed? I don't see ContentServiceCreated anywhere in the link you provided? – Aximili Feb 27 '14 at 00:08
  • You won't find it in the documentation as the implementation of EventHandlers are up to you, you subscribe to them in the same way you would any other event. I've updated my answer with a more complete example. As indicated by the event name it is executed when ANY node is created. – ProNotion Feb 27 '14 at 08:26
1

The ContentService.Created event did not work for me. Took some battles, but in v7 of Umbraco, I've used the ContentService.Saved event instead, with some double checking on dirty properties to ensure you don't end up in a saving infinite loop:

    private void ContentSaved(IContentService sender, SaveEventArgs<IContent> e)
    {
        foreach (var content in e.SavedEntities)
        {
            var dirty = (IRememberBeingDirty)content;
            var isNew = dirty.WasPropertyDirty("Id");
            if (!isNew) return;

            var parentNode = content.Parent();
            if (parentNode == null) return;
            var last = parentNode.Children().OrderBy(n => n.SortOrder).FirstOrDefault();
            if (last != null)
            {
                content.SortOrder = last.SortOrder - 1;
                if (content.Published)
                    sender.SaveAndPublishWithStatus(content);
                else
                    sender.Save(content);
            }
        }
    }

public class AppStartupHandler : ApplicationEventHandler
{
    protected override void ApplicationInitialized(UmbracoApplicationBase umbracoApplication,
        ApplicationContext applicationContext)
    {
        ContentService.Saved += ContentSaved;
    }
}
jenson-button-event
  • 18,101
  • 11
  • 89
  • 155