Remove standard Episerver modeltype options

In Episerver we might want to remove the option to create certain default ContentTypes from Episerver. But how can we do this without going to the admin panel?

Problem

We’ve created a CustomFormContainerBlock to hold an extra property: EmailRecipient. The editor must use this new block instead of the FormContainerBlock from Episerver Forms. How can we force the editor to always use this block when the FormContainerBlock is always an option when you click add on the forms panel within Episerver CMS?

We don’t want the editors to be able to still use the base form container and we don’t want to have to use the admin panel for every new environment. So how can you handle this in code?

Solution

You can extend the Episerver DefaultContentTypeAvailabilityService and remove the type from the returned list of types.

public class CustomContentTypeAvailabilityService : DefaultContentTypeAvailablilityService
{
    public CustomContentTypeAvailabilityService(
       ServiceAccessor<IContentTypeRepository> contentTypeRepositoryAccessor,
       IAvailableModelSettingsRepository modelRepository,
       IAvailableSettingsRepository typeSettingsRepository,
       GroupDefinitionRepository groupDefinitionRepository,
       IContentLoader contentLoader,
       ISynchronizedObjectInstanceCache cache) : base(contentTypeRepositoryAccessor, modelRepository, typeSettingsRepository, groupDefinitionRepository, contentLoader, cache)
    { }

    public override IList<ContentType> ListAvailable(IContent content, bool contentFolder, IPrincipal user)
    {
        var result = base.ListAvailable(content, contentFolder, user).ToList();
        return result.Where(type => type.Name != nameof(FormContainerBlock)).ToList();
    }

    public override IList<ContentType> ListAvailable(string contentTypeName, IPrincipal user)
    {
        var result = base.ListAvailable(contentTypeName, user);
        return result.Where(type => type.Name != nameof(FormContainerBlock)).ToList();
    }
}

Registering the new service

What a huge constructor! We just need to make Episerver use our new CustomContentTypeAvalibilityService. So the only thing left to do is registering the new service within the dependency resolver.

[InitializableModule]
public class DependencyResolverInitialization : IConfigurableModule
{
    public void ConfigureContainer(ServiceConfigurationContext context)
    {
        context.ConfigurationComplete += (o, e) => ConfigureContainer(e.Services);
    }
    
    private static void ConfigureContainer(IServiceConfigurationProvider provider)
    {
        provider.AddSingleton<ContentTypeAvailabilityService, CustomContentTypeAvailabilityService>();
    }
}

Voilà.