Skip to content

Instantly share code, notes, and snippets.

@PNergard
Created July 2, 2016 18:40
Show Gist options
  • Save PNergard/8296cb61b26c8bb322da7c78933e51ad to your computer and use it in GitHub Desktop.
Save PNergard/8296cb61b26c8bb322da7c78933e51ad to your computer and use it in GitHub Desktop.
Episerver initializationmodule that sorts page and block types in alfabetical order.
using System;
using System.Linq;
using System.Web;
using EPiServer.Framework;
using EPiServer.Framework.Initialization;
using EPiServer.DataAbstraction;
using System.Collections.Generic;
using EPiServer.ServiceLocation;
namespace AlloyDemoKit.Business.Initialization
{
[InitializableModule]
[ModuleDependency(typeof(EPiServer.Web.InitializationModule))]
public class ContentTypeReorderInitialization : IInitializableModule
{
public void Initialize(InitializationEngine context)
{
OrderContentTypes();
}
public void Uninitialize(InitializationEngine context)
{
//Add uninitialization logic
}
private void OrderContentTypes()
{
var pageTypeRepository = ServiceLocator.Current.GetInstance<PageTypeRepository>();
var blockTypeRepository = ServiceLocator.Current.GetInstance<BlockTypeRepository>();
var startIndex = 90;
var step = 10;
var pTypes = PageTypesList();
var bTypes = BlockTypesList();
foreach (var pType in pTypes)
{
startIndex += step;
var ptClone = pType.CreateWritableClone();
ptClone.SortOrder = startIndex;
pageTypeRepository.Save(ptClone);
}
startIndex = 90;
foreach (var bType in bTypes)
{
startIndex += step;
var btClone = bType.CreateWritableClone();
btClone.SortOrder = startIndex;
blockTypeRepository.Save(btClone);
}
}
private IEnumerable<PageType> PageTypesList()
{
return ServiceLocator.Current.GetInstance<PageTypeRepository>().List().OrderBy(p => p.Name);
//return ServiceLocator.Current.GetInstance<PageTypeRepository>().List().OrderBy(p => p.LocalizedFullName);
}
private IEnumerable<BlockType> BlockTypesList()
{
return ServiceLocator.Current.GetInstance<BlockTypeRepository>().List().OrderBy(p => p.Name);
//return ServiceLocator.Current.GetInstance<BlockTypeRepository>().List().OrderBy(p => p.LocalizedFullName);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment