How To Restrict The Blocks Allowed Within A Content Area In Episerver

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

[AllowedTypes(new []{ typeof(SlideBlock) })]
public virtual ContentArea Slides { get; set; }

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

[Display(Name = "Slides")]
[AvailableContentTypes(Include = new []{typeof(SlideBlock)})]
public virtual ContentArea Slides { get; set; }

Sometimes you may need a more custom solution. In those scenarios, you can use a ValidationAttribute or use the IValidate interface. If you want to learn more about how you can implement either of these you can read more about it here:

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

using System.Collections.Generic;
using System.Linq;
using EPiServer.Validation;
public class CarouselBlockValidator : IValidate<CarouselBlock>
{
public IEnumerable<ValidationError> Validate(CarouselBlock block)
{
var validationErrors = new List<ValidationError>();
if (block.Slides != null &&
block.Slides.Contents.Any(x => x.GetType().BaseType != typeof (SlideBlock)))
{
errors.Add(new ValidationError()
{
ErrorMessage = "Only Carousel Slides are allowed to be added in here.",
PropertyName = block.GetPropertyName<CarouselBlock >(p => p.Slides),
Severity = ValidationErrorSeverity.Error,
ValidationType = ValidationErrorType.StorageValidation
});
}
return validationErrors ;
}
}

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

0 replies

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 *