In this tutorial, you will learn about the importance of restricting what pages should be allowed to be created under each other/ Recently, one of the developers on the team had this error when he tried to do an import of content via a scheduled job.
As an Episerver architect, one of the often-overlooked requirements is making the back-end easy for editors to find the information they care about in the content tree. Having a well thought out content tree is vital in terms of creating a good user experience for content editors. One of the tools that EPiServer provides developers to help maintain the content tree, is the ability to specify what page types are allowed to be created under each other. Episerver provides two ways of doing this.
The first way is the historic method and, as a developer, I wouldn't recommend even wasting your time on it. If you load up the Episerver admin by navigating to a page types setting, selecting the 'Available Page Types' tab and adding in the page types you are able to create under it.
If you mess around restricting page types via the admin you may find caching issues and unexpected results, so I'd leave it alone. I always set this information in code via the page types definition class and this is the approach I would recommend you to follow. In order to restrict the pages that can be created under a page we use the [AvailableContentTypes] attribute, like so:
AvailableContentTypes has four properties:
- Include: The types of pages that are allowed to create under the current page type
- Exclude: The types of pages that are NOT allowed to create under the current page type
- IncludeOn: Includes the current page type to the list of available page types on the specified.
- ExcludeOn: Excludes the current page type to the list of available page types on the specified
All of these properties take an array of page types. You can think of the Include property as a white list approach to define the page types and the Exclude property as a blacklist approach. ExcludeOn and IncludeOn doesn't effect the page type in which the attribute is used. You can use a mix and match approach. From my personal experience, I've found that it's best to use either (Include/Exclude) or (IncludeOn/ExcludeOn). On all my projects, I've pretty much always just used Include on its own. In the rare times I've used Exclude it's been when I have a base page and maybe one of the children needs to behave slightly differently.
As I've only ever used the 'Include' property, after a lot of trial and error, I've found that it's usually the easiest and most straightforward approach. 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 means that only Blog Post page type can be created under the blog root page.
In today's guide, we've discussed the importance of enforcing a policy to keep the content tree in an organized fashion. Not only does it improve content editors' lives in very large sites but it can also improve performance. To restrict pages we can use the [AvailableContentTypes] attribute. I recommend only using the Includes property as I've found it's the easiest and quickest way to define your website hierarchy.