Try our conversational search powered by Generative AI!

Loading...
Area: Optimizely CMS
ARCHIVED This content is retired and no longer maintained. See the latest version here.

Recommended reading 

Restricting content types in properties

This document describes how to control the addition of certain items to a property of type ContentArea, ContentReference or ContentReferenceList (the ContentReferenceList is new from version EPiServer.CMS.UI 8.6.0).

Restricting a ContentArea

The AllowedTypes attribute (placed in the EPiServer.DataAnnotations namespace, EPiServer assembly) can be applied to a property.

[AllowedTypes(new [] { typeof(PageData) })]
public virtual ContentArea RelatedContentArea { get; set; }

The same attribute can also be set as: 

[AllowedTypes(AllowedTypes = new [] { typeof(PageData) })]
public virtual ContentArea RelatedContentArea { get; set; }


When an item that is the part of the allowed types is being dragged over this property, it is highlighted and the editor can add the item to the property. But if editor tries to drag another item which is not part of allowed types, the property is grayed and the item can't be added. The content selector dialog is also filtered out accordingly. Through the Create a new block link in the content area, editors can only add allowed item types.

You can specify several allowed types and inherited types:

[AllowedTypes(new [] {typeof(PageData), typeof(BlockData)})]
public virtual ContentArea RelatedContentArea { get; set; }


Restricting a certain set of types but allowing all others

You can also use the AllowedTypes attribute to restrict a certain set of types from a larger pool of types. 
For example, if you supply two arrays of types as constructor arguments, the first array argument considers which items are allowed, while the second argument considers restricted items. 

[AllowedTypes(new [] { typeof(BlockData) }, new [] { typeof(EditorialBlock) })]
public virtual ContentArea RelatedContentArea { get; set; }

or 

[AllowedTypes(AllowedTypes = new [] { typeof(BlockData) }, RestrictedTypes = new [] { typeof(EditorialBlock) })]
public virtual ContentArea RelatedContentArea { get; set; }


In this case, all BlockData items can be added to the content area except the EditorialBlock, which is also a BlockData but part of restricted types. When an editor drags the EditorialBlock or its sub types, the content area is grayed out. Similarly, when an editor clicks on the Create a new block link, the content selector is filtered out accordingly.

Restriction based on base classes and interfaces

Once you place the AllowTypes attribute on a ContentArea, ContentReference or ContentReferenceList property, the user interface not only allows and restricts based on the given type but also on all of the given type's sub types. For example: 

[AllowedTypes(new [] { typeof(SiteBlockData) })]
public virtual ContentArea RelatedContentArea { get; set; }

Here the property allows the SiteBlockData to be added, and all other types inherited from the SiteBlockData behave the same.

However, if you want to allow and restrict based on an interface, you must implement the UIDescriptor for the interface.

interface ISpecialInterface { } 

public class SpecialBlock : BlockData, ISpecialInterface {}

If you want to enable a content area to allow all blocks to be added except those inherited from the ISpecialInterface, first implement a UIDescriptor for the ISpecialInterface. 

[UIDescriptorRegistration]
public class SpecialInterfaceDescriptor : UIDescriptor<ISpecialInterface> { }

Then place the AllowedTypes on the content area property.

[AllowedTypes(AllowedTypes = new [] { typeof(BlockData) }, RestrictedTypes = new [] { typeof(ISpecialInterface) })]
public virtual ContentArea SomeArea { get; set; }

This code allows all blocks to be added to SomeArea but restricts blocks that implement ISpecialInterface.

Restricting content reference properties

You can also use the AllowedTypes attribute for ContentReference or ContentReferenceList properties.

[AllowedTypes( typeof(ProductPage))]
public virtual ContentReference SomeLink { get; set; }
[AllowedTypes( typeof(ProductPage))]
public virtual IList<ContentReference> SomeLinks { get; set; }

This results in the same behavior as for content areas when dragging items to the property; only items of the type “ProductPage” can be added to the property. Items not allowed (according to the AllowedTypes attribute) are not selectable in the content selector dialog.

Known limitations

  • AllowedTypes only works with ContentArea, ContentReference and ContentReferenceList type properties.
  • RestrictedTypes always win. That means if a class or interface is given in RestrictedTypes, all of its instances and/or sub items are restricted whether or not the item or sub items exist in AllowedTypes.
Do you find this information helpful? Please log in to provide feedback.

Last updated: Aug 18, 2015

Recommended reading