Sitefinity: Import Dynamic Modules Programmatically

Posted by Steve Webb on Friday, May 30, 2014

Awesome! For quite some time I have been looking for a way to do this, export a Module Builder Dynamic Module and the import that module programmatically as part of a solution. It appears that it's been available for a while at least from version 6.3 and on but I guess I never noticed it until now.

There are a few issues like the fact that you have to be logged in to execute it as well as the need (found a solution to being logged in, you can execute it with "RunWithEleveatedPrivilege" from the SystemManager) for you to extract the install.sf file from the module zip file but other than that it's a pretty straight forward exercise.

Here is the code:

private static void importDynamicModule(string moduleResource, Guid existingModuleGuid, bool activateModule)

    MemoryStream stream = Assembly.GetExecutingAssembly().ReadStreamResource(moduleResource); 
    SystemManager.RunWithElevatedPrivilege(delegate
    {
        ModuleImporter.ImportModule(stream , existingModuleGuid, activateModule);
    });
}

// Code for extension library from another team member Clinton Bale
internal static class GenericExtensions

    internal static byte[] ReadResource(this Assembly assembly, string resourcePath) 
    { 
        var stream = assembly.GetManifestResourceStream(resourcePath); 
        if (stream != null) 
        { 
            using (var ms = new MemoryStream()) 
            { 
                stream.CopyTo(ms); 
                return ms.ToArray(); 
            } 
        } 
        return null; 
    } 

    internal static MemoryStream ReadStreamResource(this Assembly assembly, string resourcePath) 
    { 
        return new MemoryStream(assembly.ReadResource(resourcePath)); 
    }
}

 

  

I have a separate class library for this code with the install.sf file from the dynamic module zip file as an embedded resource.

To get around having to be logged in I tried to use "SuppressSecurityChecks" on the providers for the ModuleBuilderManager, ConfigManager, PageManager, and the DynamicModuleManager, as you can imaging a Dynamic Module affects many different areas of Sitefinity. This approach didn't work as Sitefinity appears to be creating new managers for Pages and ModuleBuilder with a custom transaction ID (Guid). By suppressing the security checks in these providers you can get the module installed but you just can't activate it.

To make your solution cleaner you could have the install code fire from a custom config area that way you will know that the user is logged in or you could log the user in first then execute the code if you will always know of an admin user.


Go Back

Awesome! For quite some time I have been looking for a way to do this, export a Module Builder Dynamic Module and the import that module programmatically as part of a solution. It appears that it's been available for a while at least from version 6.3 and on but I guess I never noticed it until now.

There are a few issues like the fact that you have to be logged in to execute it as well as the need (found a solution to being logged in, you can execute it with "RunWithEleveatedPrivilege" from the SystemManager) for you to extract the install.sf file from the module zip file but other than that it's a pretty straight forward exercise.

Here is the code:

private static void importDynamicModule(string moduleResource, Guid existingModuleGuid, bool activateModule)

    MemoryStream stream = Assembly.GetExecutingAssembly().ReadStreamResource(moduleResource); 
    SystemManager.RunWithElevatedPrivilege(delegate
    {
        ModuleImporter.ImportModule(stream , existingModuleGuid, activateModule);
    });
}

// Code for extension library from another team member Clinton Bale
internal static class GenericExtensions

    internal static byte[] ReadResource(this Assembly assembly, string resourcePath) 
    { 
        var stream = assembly.GetManifestResourceStream(resourcePath); 
        if (stream != null) 
        { 
            using (var ms = new MemoryStream()) 
            { 
                stream.CopyTo(ms); 
                return ms.ToArray(); 
            } 
        } 
        return null; 
    } 

    internal static MemoryStream ReadStreamResource(this Assembly assembly, string resourcePath) 
    { 
        return new MemoryStream(assembly.ReadResource(resourcePath)); 
    }
}

 

  

I have a separate class library for this code with the install.sf file from the dynamic module zip file as an embedded resource.

To get around having to be logged in I tried to use "SuppressSecurityChecks" on the providers for the ModuleBuilderManager, ConfigManager, PageManager, and the DynamicModuleManager, as you can imaging a Dynamic Module affects many different areas of Sitefinity. This approach didn't work as Sitefinity appears to be creating new managers for Pages and ModuleBuilder with a custom transaction ID (Guid). By suppressing the security checks in these providers you can get the module installed but you just can't activate it.

To make your solution cleaner you could have the install code fire from a custom config area that way you will know that the user is logged in or you could log the user in first then execute the code if you will always know of an admin user.

  • Facebook
  • Twitter
  • DZone It!
  • Digg It!
  • StumbleUpon
  • Technorati
  • Del.icio.us
  • NewsVine
  • Reddit
  • Blinklist
  • Add diigo bookmark