- https://world.episerver.com/blogs/Henrik-Fransas/Dates/2015/10/the-known-and-unknown-of-include-includeon-exclude-and-excludeon/
- https://world.episerver.com/documentation/Items/Developers-Guide/Episerver-CMS/75/Content/Attributes/
The Solution
Create a standard Episerver initialization module and implement Initialize()
function.
[InitializableModule] [ModuleDependency(typeof(EPiServer.Web.InitializationModule))] public class PageRestrictionInitialization : IInitializableModule { private IContentTypeRepository _contentTypeRepository; private IAvailableSettingsRepository _availableSettingsRepository; private ILogger _log; public void Initialize(InitializationEngine context) { } public void Uninitialize(InitializationEngine context) { } }
We are using two helper functions that are making the code cleaner and easier to read and maintain.
private void DisallowAll<T>() { var page = _contentTypeRepository.Load(typeof(T)); var setting = new AvailableSetting { Availability = Availability.None }; _availableSettingsRepository.RegisterSetting(page, setting); }
DisallowAll()
function will set a page type to not have any available pages to be inserted beneath of it. We are using this function to first set the availability to none and then set the availability how we want.private void SetPageRestriction<T>(IEnumerable<Type> pageTypes) { var page = _contentTypeRepository.Load(typeof(T)); var setting = new AvailableSetting { Availability = Availability.Specific }; foreach (var pageType in pageTypes) { var contentType = _contentTypeRepository.Load(pageType); setting.AllowedContentTypeNames.Add(contentType.Name); } _availableSettingsRepository.RegisterSetting(page, setting); _log.Log(Level.Debug, $"{page.Name} restriction added."); }
SetPageRestriction()
function is where all the restrictions are done. You can pass the list of page types as a function argument and that those page types will be set as allowed page types for the passing page type type <T>.
Now we can implement the Initialize()
function.
public void Initialize(InitializationEngine context) { _contentTypeRepository = ServiceLocator.Current.GetInstance<IContentTypeRepository>(); _availableSettingsRepository = ServiceLocator.Current.GetInstance<IAvailableSettingsRepository>(); _log = LogManager.GetLogger(); var sysRoot = _contentTypeRepository.Load("SysRoot") as PageType; var setting = new AvailableSetting { Availability = Availability.Specific, AllowedContentTypeNames = { nameof(StartPage), // can't use custom interfaces with IContentTypeRepository } }; _availableSettingsRepository.RegisterSetting(sysRoot, setting); // Disallow insertion for all product and article related pages DisallowAll<ProductPage>(); DisallowAll<ArticlePage>(); DisallowAll<LandingPage>(); DisallowAll<FindSearchPage>(); // Home Page SetPageRestriction<StartPage>(new List<Type> { typeof(ContainerPage), typeof(StandardPage), typeof(LandingPage), typeof(FindSearchPage), typeof(ProductPage) }); // Landing Page SetPageRestriction<LandingPage>(new List<Type> { typeof(ArticlePage), typeof(NewsPage) }); // Container Page SetPageRestriction<ContainerPage>(new List<Type> { typeof(ContactPage) }); // Product Page SetPageRestriction<ProductPage>(new List<Type> { typeof(ContainerPage), typeof(StandardPage) }); }
I am using the Episerver Alloy demo website for this example and all the page types are from the Alloy project.
First, we get the SysRoot node as a page type and use it as a first page/node. Then we allow only the StartPage to be inserted under the root page and save the settings.
Next, we disallow inserting any page under the ProductPage, ArticlePage, LandingPage and FindSearchPage. Now we need to allow insertion for every page type how we want it. For this example I did that for the StartPage, LandingPage, ContainerPage and the ProductPage.
And that’s it.
In this way, you have a clean model for all page types and everything in one place and it’s easy to maintain and extend later on. And this concept consolidates to the first SOLID principles – Single Responsibility.