How do I programmatically disable an Orchard module?
Asked Answered
F

2

6

From Migrations.cs, I want to disable one module if it is enabled, and enable another one if it is not already enabled. How can I do this?

Frisk answered 24/10, 2012 at 19:58 Comment(0)
F
8

OK, I figured this out by looking at the Controller and Command classes in Orchard.Modules. First I had to add a project reference to Orchard.Modules, and then in Migrations.cs:

    public int UpdateFrom2() {
        var features = _moduleService.GetAvailableFeatures().ToDictionary(m=>m.Descriptor.Id, m=>m);
        if (features.ContainsKey("TinyMce") && features["TinyMce"].IsEnabled) {
            _moduleService.DisableFeatures(new string[] { "TinyMce" });
        }

        if (features.ContainsKey("TinyMceDeluxe") && !features["TinyMceDeluxe"].IsEnabled) {
            _moduleService.EnableFeatures(new string[] { "TinyMceDeluxe" });
        }

        return 3; 
    }
Frisk answered 24/10, 2012 at 20:18 Comment(2)
Just wonder how did you resolved _moduleService in Migrations?Tecla
Does not seem to work in Orchard 1.9. When i try to enable Orchard.Taxonomies it goes to state Rising in the DB and from this time the feature is not enabled and it also cannot be enabled in Admin anymore. I need to restart the webserver to get the module to state Up. Strange.Buke
C
1

I think you should take a look at the Orchard tool: you can enable and disable features and get a list of the enabled ones. Look at FeatureCommands.cs in Orchard.Modules project. Hope this helps you.

Coldblooded answered 24/10, 2012 at 20:17 Comment(1)
Thanks, that is exactly what I ended up doing.Frisk

© 2022 - 2024 — McMap. All rights reserved.