A common requirement on most websites is to restrict what blocks can be added to different content areas.  On most projects, we usually implement a carousel.  

To add a new slider into a carousel is usually done by adding a slide block into a content area within the carousel.  Any other block would break the carousels display, so locking the content area to only display slides is an important step in order to make the content editors lives as easy as possible.  

You should never aim to design a backend, where the content editor can take down the whole site, due to a simple error. If you are on Episerver 8 then the good news is that you can use the [AllowedTypes] attribute to do this for you.

AllowedTypes

The [AllowedTypes] attribute is the new Episerver 8 property that gives a lot more flexibility over the older ways of doing things. Here are a few pulled from EpiWorld

  • Validation occurs for allowed types in the content area (The old way had bugs when you dragged and dropped items)
  • Restrict a subset of the allowed types
  • Interface types are supported
  • UI descriptors are now automatically generated

In most circumstances, I would recommend using this approach. If, however, you are only using Episerver 7.5 then you will be stuck with either using the [AvailableContentTypes] attribute or implementing your own custom solution

AvailableContentTypes

Sometimes you may need a more custom solution. In those scenarios, you may want to look into the ValidationAttribute or use the IValidate interface.

To check that a blocks content area only contains a certain type of block in code rather than [AvailableContentTypes] you would use this code: