Defining The Available Page Types Allowed To Be Created Under a Page in Episerver

Recently, one of the developers on the team had this error when he tried to do an import of content via a scheduled job.

Content type "BlogPostPageType" is not allowed to be created under parent of content type "BlogRootPageType"'

So today I thought I’d write about the importance of restricting what pages should be allowed to be created under each other

Why Bother Restricting Page Types?

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 can to be 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 pages that can be create under a page we use the [AvailableContentTypes] attribute, like so:

[AvailableContentTypes(Include = new[] { typeof(BlogPostPageType) })]

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 defined 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.

Include Explained

As I’ve only ever used the Include property. after some trial and error,  I’ve found that it’s the easiest and most straight forward approach to use. 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

[AvailableContentTypes(Include = new[] { typeof(BlogPostPageType) })]
public class BlogRootType : PageData

This means that only Blog Post page type can be created under the blog root page.

Conclusion

In today’s guide we’ve discussed the importance of enforcing a policy to keep the content tree in an organised 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 sites hierarchy.

Jon D Jones

Software Architect, Programmer and Technologist Jon Jones is founder and CEO of London-based tech firm Digital Prompt. He has been working in the field for nearly a decade, specializing in new technologies and technical solution research in the web business. A passionate blogger by heart , speaker & consultant from England.. always on the hunt for the next challenge

More Posts

1 reply

Trackbacks & Pingbacks

  1. […] How To Return Different Validation Warnings To Content Editors When Publishing In EpiServer and Defining The Available Page Types Allowed To Be Created Under a Page in EpiServer to name a […]

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *