When I come across this question https://world.episerver.com/forum/developer-forum/-Episerver-75-CMS/Thread-Container/2020/3/trouble-with-availablecontenttypesattribute-excludeonincludeon/ I was rather confused by the properties of AvailableContentTypesAttribute
(admittedly I don’t use them that often!). Looking at the code that defined them, or the XML documentation does not really help. I only come to an understanding when I look into how they are used, and I guess many other developers, especially beginners, might have same confusion, so here’s a simple explanation.
Include
: defines content types that can be created as children of a content of this type (being decorated by the attribute)
IncludeOn
: defines content types that can be parent of a content of this type
Exclude
: defines content types that can not be created as children of a content of this type
ExcludeOn
: defines content types that can not be parent of a content of this type.
If there is a conflict between those properties, for example content type A has Include
with content type B, and content type B has ExcludeOn
with content type A, then Exclude
and ExcludeOn
take priority (i.e. they will override Include
and IncludeOn
. In the example above then content type B will not be able to be children of content type A)
While AvailableContentTypesAttribute
is extremely helpful, the property naming is not the best – they are short and symmetric, but they are not easy to understand and remember. An “improved” example might be
CanBeParentOf
CanBeChildrenOf
CannotBeParentOf
CannotBeChildrenOf
Yes they are more verbose, but they are unambiguous and you will not have to check the document (or this blog post) when you use them.
This is not the first time we have something that rather confusing in our API. One notable example is the old (now removed) ILinksRepository
with the Source
and Target
properties in Relation
. For quite some time I always had to check the code to know what to use, and then had the documentation updated, and eventually, changed to Parent
and Child
. No API is created perfect, but we can improve over time.
Hi Quan Mai.
Thank you so much for this explanation von include, includeOn, exclude and excludeOn.
The explanation in the CMS Developer guides confused me and almost drove me crazy 😉
Many greeting from Germany,
Tim
Glad I could help!
Hi Quan Mai
I’ve seen using of this on blocks like this:
[AvailableContentTypes(IncludeOn = new Type[] { typeof(ParentPage) })]
public class FirstBlock : BlockData
and also
public class SecondBlock : BlockData
having ParentPage containing ContentArea:
[AllowedTypes(typeof(FirstBlock), typeof(SecondBlock), typeof(IMainContentBlock))]
public virtual ContentArea MainContent { get; set; }
What types would you expect to be able to create in MainContent on ParentPage?
Best
Miroslav