Restrict content types in properties
Describes how to control the addition of certain items to a property of type ContentArea, ContentReference or ContentReferenceList.
Restrict a ContentArea
Control which content types editors add to a ContentArea property by applying the AllowedTypes attribute. The AllowedTypes attribute (in the EPiServer.DataAnnotations namespace, EPiServer assembly) applies to a property.
As the examples below show, the array declaration is not needed.
[AllowedTypes(new [] {
typeof (PageData)
})]
public virtual ContentArea RelatedContentArea {
get;
set;
}
[AllowedTypes(typeof (PageData))]
public virtual ContentArea OtherRelatedContentArea {
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 part of the allowed types is dragged over this property, the property highlights and accepts the item. If the editor drags an item that is not part of the allowed types, the property grays out and rejects the item. The content selector dialog filters accordingly. Through the Create a new block link in the content area, editors add only allowed item types.
Specify several allowed types and inherited types:
[AllowedTypes(new [] {
typeof (PageData), typeof (BlockData)
})]
public virtual ContentArea RelatedContentArea {
get;
set;
}
[AllowedTypes(typeof (PageData), typeof (BlockData))]
public virtual ContentArea OtherRelatedContentArea {
get;
set;
}Restrict a certain set of types but allow others
Exclude specific types from a broader allowed set using the RestrictedTypes parameter. Supply two arrays of types as constructor arguments: the first array specifies allowed items, while the second specifies 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, the content area accepts BlockData items except EditorialBlock, which is a BlockData but also part of restricted types. When an editor drags EditorialBlock or its subtypes, the content area grays out. When an editor clicks the Create a new block link, the content selector filters accordingly.
Restrict based on base classes and interfaces
Apply type restrictions that automatically extend to subtypes and interfaces. Placing the AllowedTypes attribute on a ContentArea, ContentReference, or list of ContentReference property restricts based on both the given type and its subtypes. For example:
[AllowedTypes(new [] {
typeof (SiteBlockData)
})]
public virtual ContentArea RelatedContentArea {
get;
set;
}In this case, the property not only adds the SiteBlockData but other types inherited from the SiteBlockData also behave the same.
To allow and restrict based on an interface, implement the UIDescriptor for the interface.
interface ISpecialInterface { }
public class SpecialBlock : BlockData, ISpecialInterface {}To enable a content area to add blocks (except those inheriting from ISpecialInterface), first implement a UIDescriptor for ISpecialInterface.
[UIDescriptorRegistration]
public class SpecialInterfaceDescriptor : UIDescriptor<ISpecialInterface> { }Additionally, the interface must inherit from the IContentData interface.
public interface ISpecialInterface: IContentData {
// properties and methods
}After that, 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 adds blocks to SomeArea but restricts blocks that implement ISpecialInterface.
Restrict content reference properties
Apply the same type restrictions to ContentReference and list of ContentReference properties:
[AllowedTypes(typeof (ProductPage))]
public virtual ContentReference SomeLink {
get;
set;
}
[AllowedTypes(typeof (ProductPage))]
public virtual IList<ContentReference> SomeLinks {
get;
set;
}This code produces the same drag-and-drop behavior as content areas. Only items of type ProductPage are accepted. Items not allowed (according to the AllowedTypes attribute) are not selectable in the content selector dialog.
Known limitations
Review these constraints before implementing type restrictions.
AllowedTypesonly works withContentArea,ContentReferenceand list ofContentReferenceproperties.RestrictedTypesalways win. That means that if an item (a class or an interface) is given inRestrictedTypes, its instances and sub-items are restricted whether or not the item or sub-items exist inAllowedTypes.
Updated 17 days ago
