Try our conversational search powered by Generative AI!

Remove Suggested Page Types in CMS 12

Vote:
 

In the CMS 11 training exercises, there is a lab to remove the Suggested Page Types for editors.

Create a ConfigurableModule

[InitializableModule]
    [ModuleDependency(typeof(EPiServer.Web.InitializationModule))]
    public class RemoveSuggestedPageTypesInitializationModule : IConfigurableModule
    {
        public void ConfigureContainer(ServiceConfigurationContext context)
        {
            context.StructureMap().EjectAllInstancesOf<IContentTypeAdvisor>();
        }
        public void Initialize(InitializationEngine context)
        { }
        public void Uninitialize(InitializationEngine context)
        { }
    }

In CMS 12, the StructureMap DI is no longer used, so I'm unclear on how to remove or eject a configurable module in CMS 12.


#269363
Jan 03, 2022 18:09
Vote:
 

I think I found the answer, thanks to: https://stackoverflow.com/questions/42945859/remove-a-service-in-asp-net-core-dependency-injection/42946802#answer-42946802

[InitializableModule]
[ModuleDependency(typeof(EPiServer.Web.InitializationModule))]
public class RemoveSuggestedPageTypesInitialization : IConfigurableModule
{
    public void ConfigureContainer(ServiceConfigurationContext context)
    {
        var serviceDescriptor = context.Services
            .FirstOrDefault(descriptor => descriptor.ServiceType == typeof(IContentTypeAdvisor));

         if (serviceDescriptor != null)
            context.Services.Remove(serviceDescriptor);
    }
    public void Initialize(InitializationEngine context) { }
    public void Uninitialize(InitializationEngine context) { }
}

This works. I'm not sure if it is the "correct" approach.

#269364
Jan 03, 2022 18:21
Mattias Olsson - Jan 03, 2022 18:38
I'm curious. Could you try only this line to see if you also get an exception during startup:
context.Services.RemoveAll

Edit: Hmm, I see that "less than" and "greater than" was not allowed in comments. But you get what I mean anyway I hope. :)
Vote:
 

Hi!

Try this in ConfigureContainer method:

context.Services.RemoveAll<IContentTypeAdvisor>();
context.Services.TryAddEnumerable(ServiceDescriptor.Singleton<IContentTypeAdvisor, EmptyContentTypeAdvisor>());

EmptyContentTypeAdvisor:

public class EmptyContentTypeAdvisor : IContentTypeAdvisor
{
    public IEnumerable<int> GetSuggestions(IContent parent, bool contentFolder, IEnumerable<string> requestedTypes)
    {
        return Enumerable.Empty<int>();
    }
}
#269365
Jan 03, 2022 18:32
Vote:
 

I got an exception during startup when I removed all instances of IContentTypeAdvisor, so that's why I added that empty implementation. Maybe I got the exception because because I tested it on a Commerce site.

#269366
Jan 03, 2022 18:34
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.