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

A common requirement in any website is to restrict what blocks can be added into different content areas, for example, on most projects we usually implement a carousel. Carousels are populated with slide bocks and only a slide block should be allowed within the carousel blocks content area.

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 using 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 *