I would like to keep a specific DocType at the top of the tree that contains it, even after new content is added or the sort order is manually changed.
I am using V6.1.6 - which event(s) would I need to monitor in order to catch anything that may change the position of this DocType (that I want to keep at the top)?
Here is a little snippet I've used in the past to sort home page children:
using System.Linq;
using Umbraco.Core;
using Umbraco.Core.Events;
using Umbraco.Core.Models;
using Umbraco.Core.Services;
namespace Site.Events
{
public class HomePageSortEvent : ApplicationEventHandler
{
public HomePageSortEvent()
{
ContentService.Saved += ContentServiceOnSaved;
}
private static void ContentServiceOnSaved(IContentService sender, SaveEventArgs e)
{
var home = e.SavedEntities.First().Parent();
if (home.ContentType.Alias.Equals("HomePage") && home.Children().Any())
{
var children = home.Children();
// Some logic to reorder home page children... For example... children = children.OrderBy(d => d.Name);
sender.Sort(children, raiseEvents: false);
umbraco.BasePages.BasePage.Current.ClientTools.SyncTree(homePage.Path, true);
}
}
}
}
Fix particular DocType at the top in content tree
I would like to keep a specific DocType at the top of the tree that contains it, even after new content is added or the sort order is manually changed.
I am using V6.1.6 - which event(s) would I need to monitor in order to catch anything that may change the position of this DocType (that I want to keep at the top)?
Is this a reasonable solution?
The above is in a function attached to the "ContentService.Saved" event.
Here is a little snippet I've used in the past to sort home page children:
using System.Linq; using Umbraco.Core; using Umbraco.Core.Events; using Umbraco.Core.Models; using Umbraco.Core.Services; namespace Site.Events { public class HomePageSortEvent : ApplicationEventHandler { public HomePageSortEvent() { ContentService.Saved += ContentServiceOnSaved; } private static void ContentServiceOnSaved(IContentService sender, SaveEventArgs e) { var home = e.SavedEntities.First().Parent(); if (home.ContentType.Alias.Equals("HomePage") && home.Children().Any()) { var children = home.Children(); // Some logic to reorder home page children... For example...children = children.OrderBy(d => d.Name); sender.Sort(children, raiseEvents: false); umbraco.BasePages.BasePage.Current.ClientTools.SyncTree(homePage.Path, true); } } } }
In your case, you'd probably want something like:
using System; using System.Linq; using Umbraco.Core; using Umbraco.Core.Events; using Umbraco.Core.Models; using Umbraco.Core.Services; namespace Application { public class SortEvent : ApplicationEventHandler { public SortEvent() { ContentService.Saved += ContentServiceOnSaved; } private static void ContentServiceOnSaved(IContentService sender, SaveEventArgs e) { try { foreach (var content in e.SavedEntities) { var parent = content.Parent(); if (parent == null) continue; var children = parent.Children() .Where(p => p.ContentType.Alias.Equals(“ContentContainer”)) .OrderBy(c => c.Name) .ToList(); children.AddRange(parent.Children() .Where(p => !p.ContentType.Alias.Equals(“ContentContainer”)) .OrderBy(c => c.Name) .ToList()); sender.Sort(children, raiseEvents: false); umbraco.BasePages.BasePage.Current.ClientTools.SyncTree(content.Path, true);} } catch (Exception) { } } } }
is working on a reply...
This forum is in read-only mode while we transition to the new forum.
You can continue this topic on the new forum by tapping the "Continue discussion" link below.