Published on: 10 October, 2013

How to uninstall DNN Module programmatically?

Tumblr Digg Google Plus

To uninstall the DNN module is actually quite complex via code. There are couple of table modules related and depends on each other. Fortunately, the DNN framework has a built in delete method for these modules. There are 3 tables in the database that are related to the module definitions. They are: DesktopModules, ModuleDefinitions and PortalDesktopModules. The DesktopModules and ModuleDefinitions tables are global tables that are linked to all portals. While PortalDesktopModules table contains a relationship between each portals and what modules can be used by each individual portal.

Lets say you want to remove a list of modules containing a name started with MyModule_.

/* Firstly, you need to include these namespaces on the top of the code */
/* This namespace for Dictionary collection */
using System.Collections;
/* These namespaces for module collection */
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Modules.Definitions;

The next part is to grab a list of module definitions collection and compare if the name contains MyModule_

/* Declare ModuleDefinitionController object */
ModuleDefinitionController objModuleDefinitionController = new ModuleDefinitionController();

/* Declare DesktopModuleController object */
DesktopModuleController objDesktopModuleController = new DesktopModuleController();

/* Get dictionary module collection */
Dictionary<int, ModuleDefinitionInfo>  moduleList = ModuleDefinitionController.GetModuleDefinitions();

/* Perform a looping in the module list */
foreach (int i in moduleList.Keys) {
	/* We only delete the module friendly name contains MyModule_ */
	if (moduleList[i].FriendlyName.IndexOf("MyModule_") >= 0) {
		/* Delete the module from table module definitions */
		objModuleDefinitionController.DeleteModuleDefinition(moduleList[i].ModuleDefID);

		/* Delete the module from table desktop modules */
		objDesktopModuleController.DeleteDesktopModule(moduleList[i].DesktopModuleID);

		/* Delete the module from portal desktop modules */
		DesktopModuleController.RemoveDesktopModuleFromPortal(PortalId, moduleList[i].DesktopModuleID, true);
	}
}

Comments

There are no comments available.

Write Comment
0 characters entered. Maximum characters allowed are 1000 characters.