In this tutorial, you will learn how to restrict what page types can be created under each other within Episerver CMS. An often-overlooked part of being an Episerver architect, is to design the backend so that it is easy for content editors to use. It is important to provide editors with some guidance on how they should build content within the content tree. Restricting what page types can be created within the page tree is a great way of accomplishing this.

Episerver provides two ways of restricting content, one way is within the CMS and the other is in code. I do not recommend that you use the UI approach as it makes it really hard to keep changes between your local, development, staging and production environments in sync. You may also find caching issues and unexpected results. To make a restriction this way, within the Episerver admin UI, select the page that you want to edit within the Available Page Type tab. From this screen you can add the restrictions.

I apply restrictions in my projects using the code first approach and this is the technique I would recommend you follow. In order to restrict the page types this way you will use the [AvailableContentTypes] attribute within our page type definitions, like so:

[AvailableContentTypes] has four properties. To use these properties in code, you can pass in an array of page types into the attribute. Each property applies a slightly different type of restriction:

  • Include: The types of pages that are allowed to be created under the current page type. In essance, a white list approach to define the page types
  • Exclude: The types of pages that are NOT allowed to be created under the current page type. A blacklist approach to define the pages
  • IncludeOn: An inclusive list of all the locations where the page type can be used
  • ExcludeOn: An exclusive list of all where the page types is NOT allowed to be used

From my personal experience, I've found that it's best to use either (Include/Exclude) or (IncludeOn/ExcludeOn). It is possible to use a mix and match approach to these properties, although I advise against it.

On my projects, I've pretty much always just used Include as it results in the cleanest code and its easy to use. In some rare times I've usedExclude, mainly on a base class. I tend to avoid IncludeOn/ExcludeOn

When you use Include, you are specifying the pages that can be created under the current page type definition. Going back to the snippet above:

This restriction means that pages of BlogPost can be created under the blog root page and nothing else.

To restrict pages we can use the [AvailableContentTypes] attribute. Out of the options that [AvailableContentTypes] provides, I recommend using the Includes property. I've found Includes is the easiest and quickest way to define your website hierarchy. On every project before launching, go through every page type and make sure you have added this attribute and configured it correctly. Happy Coding 🤘