Optimizely CMS 12: Select Content spins forever (AllowedTypes)
A content area limited to a marker interface showed an endless spinner in Select Content. The cause was one missing base interface, and the real error was one request away.
I was adding content areas to a page in Optimizely CMS 12, each restricted to one group of blocks. When I opened the Select Content dialog to pick a block, the tree showed its two roots with loading markers that never went away.
I guessed it was search indexing. It was not. The request behind the dialog was throwing, and the error named the exact cause:
System.Security.VerificationException: Method EPiServer.Core.Internal.DefaultContentRepository.GetChildren:type argument 'CardSite.Models.Blocks.ICardBlock' violates the constraint of type parameter 'T'.What a content area and AllowedTypes are
A content area is a property that holds a list of content items, usually blocks. An editor drops blocks into it in the edit UI, and the page template renders them in order. In code it is a property of type ContentArea.
A block is a reusable piece of content, such as a card or a banner. Each block has its own C# class.
By default an editor can put any block in any content area. The [AllowedTypes] attribute narrows that. Optimizely’s documentation says it works on ContentArea, ContentReference and ContentReferenceList properties, and that without it any item can be added. With it, the editor’s picker is filtered to the types you list.
You can list concrete classes. That gets awkward when five different block classes should all be allowed. So a common pattern is a marker interface: an interface with no members, or a few shared ones, that every block in the group implements. The area allows the interface, and any block that implements it qualifies.
This is the broken version. The interface is a plain C# interface, the page restricts its area to it, and a block implements it:
public interface ICardBlock{}[AllowedTypes(typeof(ICardBlock))]public virtual ContentArea Cards { get; set; }[ContentType(DisplayName = "Promo card")]public class PromoCardBlock : BlockData, ICardBlock{}That compiles, the page type syncs, and the page renders. Only the editor’s picker breaks.
What went wrong
I opened Select Content on a restricted area. The tree showed its roots, and their children never loaded.
My first guess was indexing. That was wrong, and a moment’s thought would have shown it: the dialog lists content that is already in the database, not results from a search index.
The second guess was a cold start, because the site had just restarted and a request to the home page timed out. The next one answered in 47 milliseconds, and the dialog still failed. Both guesses cost time before anyone looked at the failing request.
The request that fills the tree is the one to read. Open the browser developer tools, go to the Network tab, reopen the dialog, and find the call to the content store. A 500 response there means the server threw. On a local site running in Development, ASP.NET Core’s developer exception page puts the full exception in that response.
Mine started like this, with frames in between left out:
System.ArgumentException: GenericArguments[0], 'CardSite.Models.Blocks.ICardBlock', on'System.Collections.Generic.IEnumerable`1[T] GetChildren[T](EPiServer.Core.ContentReference, EPiServer.Core.LoaderOptions)'violates the constraint of type 'T'. ---> System.Security.VerificationException: Method EPiServer.Core.Internal.DefaultContentRepository.GetChildren:type argument 'CardSite.Models.Blocks.ICardBlock' violates the constraint of type parameter 'T'. ... at System.Reflection.RuntimeMethodInfo.MakeGenericMethod(Type[] methodInstantiation) at EPiServer.Cms.Shell.UI.Rest.ContentQuery.ContentQueryBase.GetChildrenByType(Type type, ContentReference parentLink, ILanguageSelector selector) at EPiServer.Cms.Shell.UI.Rest.ContentQuery.GetChildrenQuery.<>c__DisplayClass13_0.<GetChildren>b__0(ContentTypeRecord type) ... at EPiServer.Cms.Shell.UI.Rest.ContentQuery.GetChildrenQuery.GetContent(ContentQueryParameters parameters) ... at EPiServer.Cms.Shell.UI.Rest.Internal.ContentStore.Get(...)Read it from the bottom up. The editor calls the content store. The store runs a children query, which takes a type, tries to build GetChildren<T> for it with MakeGenericMethod, and the runtime refuses. GetChildren itself never runs: the exception comes from MakeGenericMethod, before any content is loaded.
Why the runtime refuses
GetChildren<T> is a generic method: one method that works for any type you put in place of T. The overload in the stack, as Optimizely’s class library documents it on IContentLoader, is:
IEnumerable<T> GetChildren<T>(ContentReference contentLink, LoaderOptions settings) where T : IContentDataEvery GetChildren<T> overload carries the same rule. The where T : IContentData part is a generic type constraint: T has to be IContentData or something derived from it.
Normally the compiler checks that. Here the editor does not know the type until runtime, so it builds the call by reflection, and the check happens then. An interface with no base interface is not an IContentData, so the check fails and the request returns an error.
Here is what the evidence shows. The exception text proves the failing call and the failing constraint, and the spinner stopped after the change below. That the dialog does this once per allowed type is my reading of the stack, which I did not test. Optimizely’s support sample for interfaces with AllowedTypes on CMS 12 also says the interface should inherit from IContentData, which matches.
The fix
Make each marker interface that appears in AllowedTypes inherit IContentData. The type lives in EPiServer.Core.
using EPiServer.Core;
namespace CardSite.Models.Blocks;
public interface ICardBlockpublic interface ICardBlock : IContentData{}Nothing else changes. The block classes, the attribute and the page type stay as they were. On that project six interfaces had the same problem and I changed all six. The interfaces already in the codebase did it correctly, for example IBlock : IPartial, IContentData, and I had not copied them.
How to check it worked
- Rebuild and restart the site.
- Hard refresh the edit UI (
Ctrl+F5) and open Select Content on the restricted area. - The spinner should stop. In the Network tab the content store call should return 200.
In my case the spinner stopped straight away. That is as far as this fix goes.
One honest note: the picker then showed nothing to select under the roots. A block I created afterwards would not drag into the area, and later Select Content was not working on the other restricted areas either. I moved on to other work without finding the cause, so I cannot tell you what it was. An empty picker with no error is not this bug: this one fails loudly, in the request, so read the request first.
Optimizely’s documentation on restricting content types names a second step that I did not try: to restrict by an interface, register a UI descriptor for it (a class deriving from UIDescriptor<ICardBlock> that tells the edit UI about the type). Its CMS 12 support sample does the same. If the picker loads but blocks will not drop into the area, that is where I would look next.
If the site does not run on your machine yet, I listed the seven errors I hit running CMS 12 and Commerce locally.
Takeaways
- A spinner in Select Content is a failed request. Open the Network tab and read the response.
- The dialog reads the content database, not the search index.
- Every interface used in
[AllowedTypes]must inheritIContentData. GetChildren<T>is constrained toIContentData, and the editor builds that call at runtime, so the compiler never warns you.- A picker that loads but stays empty is a different problem. A missing UI descriptor is my first, untested, suspect.
- Copy the pattern from the interfaces already in the codebase before inventing your own.