Copied to clipboard

Flag this post as spam?

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


  • Gordon Saxby 1347 posts 1608 karma points
    Nov 18, 2013 @ 12:54
    Gordon Saxby
    0

    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)?

  • Gordon Saxby 1347 posts 1608 karma points
    Nov 18, 2013 @ 15:16
    Gordon Saxby
    0

    Is this a reasonable solution?

                foreach (var content in e.SavedEntities)
    {
    if (content.ContentType.Alias == "ContentContainer")
    {
    content.SortOrder = 0;
    }
    else
    {
    content.SortOrder++;
    }

    // Only Publish if already published
    if (content.Published)
    {
    sender.SaveAndPublish(content, 0, false);
    }
    else
    {
    sender.Save(content, 0, false);
    }
    }

    The above is in a function attached to the "ContentService.Saved" event.

  • Dan Lister 416 posts 1973 karma points c-trib
    Nov 21, 2013 @ 13:28
    Dan Lister
    0

    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); } } } }
  • Dan Lister 416 posts 1973 karma points c-trib
    Nov 21, 2013 @ 14:09
    Dan Lister
    0

    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) { } } } }
  • 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.

Please Sign in or register to post replies