Build an Episerver CMS 9 Initialization Module to Sort Custom Tabs

First, I want to give credit for the guts of the code here to Per Magne Skuseth, an Episerver employee and frequent EpiserverWorld contributor. He originally posted the code to sort custom tabs in this EpiserverWorld post. I wanted to take it a step further and show you the code to wrap it in a custom Episerver Initialization Module. I only recently started messing around with EPiServer 9 and if you were wondering, building a custom initialization module has not changed from previous versions.

 

So the premise for this is really the need to sort content building tabs in custom page and block types. I like to build the back-end interface for creating content in a way that is easy to use and simple to understand. This is usually achieved by braking the sections of a block or page into tabs. One thing I am trying to do with all the articles I post is briefly talk about the Visual Studio project setup. I am using Visual Studio 2015 and I have installed the latest version of EPiServer CMS 9 (version: 9.3.1 as of the time of this post) via the EPiServer Visual Studio extension. Inside of the Business folder that gets installed by default, I create a folder called Initialization to hold my custom initialization modules. For this module, just right-click on the folder and create a custom C# class called TabSortOrder.

 

Visual Studio EPiServer project setup

 

The code to create the custom module is pretty easy. All you need to do is inherit from : IInitializableModule and add this to the top of the class declaration:

 

[ModuleDependency(typeof(EPiServer.Web.InitializationModule))]

 

Inside of the private void method RegisterTabs is where you list the custom tabs and their sort order. The rest of the code is pretty self explanatory.

 

using EPiServer.Core;
using EPiServer.DataAbstraction;
using EPiServer.Framework;
using EPiServer.Framework.Initialization;
using EPiServer.Security;
using EPiServer.ServiceLocation;
using EPiServer.Shell.ObjectEditing;
using System;
using System.Linq;

namespace YourNamespace.Business.Initialization
{
    /// 
    /// Module for setting the order of custom model tabs
    /// 
    [ModuleDependency(typeof(EPiServer.Web.InitializationModule))]
    public class TabSortOrder : IInitializableModule
    {
        public void Initialize(InitializationEngine context)
        {
            RegisterTabs();
            if (context.HostType == HostType.WebApplication)
            {
                var registry = context.Locate.Advanced.GetInstance();
                registry.RegisterMetadataHandler(typeof(ContentData), new SiteMetadataExtender());
            }
        }

        private void RegisterTabs()
        {
            var tabDefinitionRepository = ServiceLocator.Current.GetInstance<ITabDefinitionRepository>();

            this.AddTabToList(tabDefinitionRepository,
                new TabDefinition() { Name = "Hero", RequiredAccess = AccessLevel.Edit, SortIndex = 27 });

            this.AddTabToList(tabDefinitionRepository,
                new TabDefinition() { Name = "SEO", RequiredAccess = AccessLevel.Edit, SortIndex = 28 });

            this.AddTabToList(tabDefinitionRepository,
                new TabDefinition() { Name = "Site Settings", RequiredAccess = AccessLevel.Edit, SortIndex = 29 });    
        }

        private void AddTabToList(ITabDefinitionRepository tabDefinitionRepository, TabDefinition definition)
        {
            TabDefinition existingTab = GetExistingTabDefinition(tabDefinitionRepository, definition);
            if (existingTab != null)
            {
                definition.ID = existingTab.ID;
            }
            tabDefinitionRepository.Save(definition);
        }
        private static TabDefinition GetExistingTabDefinition(ITabDefinitionRepository tabDefinitionRepository, 
TabDefinition definition)         {             return tabDefinitionRepository.List()                    .FirstOrDefault(t => t.Name.Equals(definition.Name, StringComparison.InvariantCultureIgnoreCase));         }         public void Uninitialize(InitializationEngine context)         {         }         public void Preload(string[] parameters)         {         }     } }

 

That is all there is to it. One thing to keep in mind is that Episerver keeps a custom sort order on their system tabs. Here is the sort for your reference:

 

SystemTabNames.Content = SortIndex 10
SystemTabNames.Scheduling = SortIndex 20
SystemTabNames.Settings = SortIndex 30
SystemTabNames.Shortcut = SortIndex 40
SystemTabNames.Categories = SortIndex 50

 

Hopefully this will help you to get going with sorting custom tabs in Episerver 9. I just recently started developing in Episerver and really like it so far. With all the craziness going on in the world right now, I would encourage everyone to always keep learning and doing what you love. I really love writing code and thinking about/solving programming logic problems. Hit me up if you have any question or comments. Also, check out some of my public GitHub repos here

0 comment(s) in response to Build an Episerver CMS 9 Initialization Module to Sort Custom Model Tabs

Have a comment?