episerveroptimizely

Register an addon in Episerver CMS 12


Plugin UI are developed in a separate MVC project and CMS 12 is in another projects. Following is a test solution that just to explain the issue we are having. Solution structure

Please consider followings

  1. The TestAddon project is a Simple MVC project with basic UI. We need to get this UI rendered in a CMS 12 Admin menu. We have created a menu provider as well.
  2. Then build the TestAddon project and copied DLLs to CMS-> bin folder.
  3. Created module/_protected folder and added TestAddon/TestAddon.zip
  4. module.config was created as described in the documentation(https://world.optimizely.com/documentation/developer-guides/CMS/configuration/Configuring-moduleconfig/)
    <module productName="TestAddon" loadFromBin="false" tags="EPiServerModulePackage" clientResourceRelativePath="1.0.0">
      <assemblies>
        <add assembly="TestAddon" />
      <add assembly="TestAddon.Views" />
      </assemblies>
    
      <route url="{controller}/{action}" > 
        <defaults>
          <!--<add key="moduleArea" value="TestAddon" />-->
          <add key="controller" value="CustomAdminPage" />
          <add key="action" value="Index" />
        </defaults>
      </route>
    
      <clientResources>
        <!-- <add name="myscript" path="ClientResources/index.js" resourceType="Script" ></add> -->
      </clientResources>
    
    
      <clientModule>
        <moduleDependencies>
          <add dependency="CMS" />
          <add dependency="Shell"/>
           <add dependency="EPiServer.Cms.UI.Admin" type="RunAfter"/>
          <add dependency="EPiServer.Cms.UI.Settings" type="RunAfter"/> 
        </moduleDependencies>
        <requiredResources>
        </requiredResources>
      </clientModule>
    </module>
  1. Set Auto discovery in startup file

services.Configure<ProtectedModuleOptions>(x => x.AutoDiscovery = EPiServer.Shell.Configuration.AutoDiscoveryLevel.Modules);

  1. When we then start the project it is giving following error Error Screenshot

  2. Stacktrace

  3. When we removed the auto discovery setting form startup class. It works to build the project

Does anyone have experienced this? Please point me in a correct direction


Solution

  • You don't need to copy files to your sample project for local testing. You can add a project reference to your add-on project instead, then add this in your sample project's startup so the files are loaded correctly:

    var moduleName = typeof(SomeClassInYourAddOn).Assembly.GetName().Name;
    
    services.Configure<CompositeFileProviderOptions>(options =>
    {
        options.BasePathFileProviders.Add(new MappingPhysicalFileProvider(
            $"/EPiServer/{moduleName}",
            string.Empty,
            Path.GetFullPath($"..\\..\\src\\{moduleName}")));
    });
    
    services.Configure<ProtectedModuleOptions>(options =>
    {
        options.Items.Add(new ModuleDetails { Name = moduleName });
    });
    

    Not sure if this is needed, but I don't think protected modules are auto discovered. So if you have a configuration method in your add-on that consumers of your add-on need to call, then you can add this in the method:

    var moduleName = typeof(SomeClassInYourAddOn).Assembly.GetName().Name;
    
    services.Configure<ProtectedModuleOptions>(options =>
    {
        if (!options.Items.Any(i => i.Name.Equals(moduleName, StringComparison.OrdinalIgnoreCase)))
        {
            options.Items.Add(new ModuleDetails() { Name = moduleName });
        }
    });
    

    Then your add-on is added even if auto discovery is not enabled.