+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Import module builder export programmatically?

Import module builder export programmatically?

4 posts, 0 answered
  1. Basem
    Basem avatar
    131 posts
    Registered:
    22 Dec 2010
    03 Mar 2013
    Link to this post

    I am trying to import a module builder export programmatically, but the API in Sitefinity is marked as internal: Telerik.Sitefinity.DynamicModules.Builder.ExportImport.ModuleImporter. I can read the zip file from a location in the file system, but can't import it programmatically. Any idea on how to get around this?

  2. Bob
    Bob avatar
    25 posts
    Registered:
    14 Aug 2012
    29 Apr 2013 in reply to Basem
    Link to this post
    Hello,

    I'm in the same situation, did you find a solution?

    kind regards,

    bob
  3. Basem
    Basem avatar
    131 posts
    Registered:
    22 Dec 2010
    29 Apr 2013
    Link to this post
    Unfortunately I have not and ticket support had none either.

    The problem is the class/method needed to do this is marked as private in the SF source code (the same method used to "import a module" from the admin.
  4. Bob
    Bob avatar
    25 posts
    Registered:
    14 Aug 2012
    17 May 2013 in reply to Basem
    Link to this post
    Hello,

    I found a solution, it's not 100% because we have to indeed call internal stuff, but maybe it can help someone.

    private static void ImportDynamicModule()
    {
        Stream zipFileAsStream = ResourceHelper.ReadResourceFile(typeof(InstallCmCareModule), "CmCare.zip");
     
        using (var moduleZipFile = ZipFile.Read(zipFileAsStream))
        {
            ReflectionUtilities.CallNonPublicStatic(typeof(App), "Telerik.Sitefinity.DynamicModules.Builder.ExportImport.ModuleImporter", "ImportModule", moduleZipFile);
        }
    }
     
    private static void ActivateModule()
    {
        PageManager.GetManager().Provider.SuppressSecurityChecks = true;
        ConfigManager.GetManager().Provider.SuppressSecurityChecks = true;
     
        string providerName = DynamicModuleManager.GetDefaultProviderName();
        ReflectionUtilities.CallMethodOnInternalClass("Telerik.Sitefinity.DynamicModules.Builder.Web.Services.ContentTypeService", "ActivateModule", Constants.Guids.CmCareModuleId.ToString(), providerName);
     
        ConfigManager.GetManager().Provider.SuppressSecurityChecks = false;
        PageManager.GetManager().Provider.SuppressSecurityChecks = false;
    }

    And the helpers
    public static object CallNonPublicStatic(Type typeFromAssembly, string typeToResolve, string methodName, params object[] paramaters)
    {
        if (string.IsNullOrWhiteSpace(typeToResolve))
        {
            throw new ArgumentNullException("typeToResolve");
        }
     
        if (string.IsNullOrWhiteSpace(methodName))
        {
            throw new ArgumentNullException("methodName");
        }
     
        var type = typeFromAssembly.Assembly.GetType(typeToResolve);
     
        if (type == null)
        {
            throw new ArgumentException(string.Format("Type '{0}' not found.", typeToResolve));
        }
     
        MethodInfo methodInfo = type.GetMethod(methodName);
     
        if (methodInfo == null)
        {
            throw new ArgumentException(string.Format("Method '{0}' not found on type '{1}'.", methodName, typeToResolve));
        }
     
        return methodInfo.Invoke(null, paramaters);
    }
     
    public static object CallMethodOnInternalClass(string fullQualifiedName, string methodName, params object[] parameters)
    {
        if (string.IsNullOrWhiteSpace(fullQualifiedName))
        {
            throw new ArgumentNullException("fullQualifiedName");
        }
     
        if (string.IsNullOrWhiteSpace(methodName))
        {
            throw new ArgumentNullException("methodName");
        }
     
        Assembly assembly = typeof(App).Assembly;
        Type internalClass = assembly.GetType(fullQualifiedName, true, true);
     
        if (internalClass == null)
        {
            throw new ArgumentException(string.Format("Type '{0}' not found.", fullQualifiedName));
        }
     
        var instance = Activator.CreateInstance(internalClass);
        MethodInfo methodInfo = instance.GetType().GetMethod(methodName);
         
        if (methodInfo == null)
        {
            throw new ArgumentException(string.Format("Method '{0}' not found on type '{1}'.", methodName, fullQualifiedName));
        }
     
        return methodInfo.Invoke(instance, parameters);
    }

    Bye,

    bob
4 posts, 0 answered