Custom Modules - Part 9 - Auto Registering Modules
Auto Registering your module
As shown in a forum post on the Engage web site by Morten Bengtson, all you need to register a module programmatically is a few lines of code. Here's Morten's original code with a small change - I also added the Name parameter so you can actually see and click the module in the Modules list in the Management Center:
var moduleSystemName = "MyAmazingModule"; if (!Base.IsModuleInstalled(moduleSystemName, true)) { var module = new Dynamicweb.RegisterModules.Module { Name = "My Amazing Module", SystemName = moduleSystemName, Description = "bla bla", Script = "/CustomModules/MyAmazingModule/MyAmazingModule_Backend.aspx", ControlPanel = "/CustomModules/MyAmazingModule/MyAmazingModule_Cpl.aspx" }; module.Save(); }
Once this code has executed, the module is registered and appears in the list of modules.
You're not limited to just registering the module though. You can also register the search settings in one fell swoop. For more information about registering your module with Dynamicweb Search, check out this article on the Engage web site.
Here's a complete example that registers the module and its search settings:
public static void RegisterAutoRegisterModule() { var moduleSystemName = "AutoRegisterModule"; if (!Base.IsModuleInstalled(moduleSystemName, true)) { var module = new Dynamicweb.RegisterModules.Module { Access = true, Paragraph = true, Name = "Auto Register Module", // Important or you won't be able to click // the module in the list SystemName = moduleSystemName, Description = "Your module description", Script = string.Format("/CustomModules/{0}/Admin/", moduleSystemName), SearchSettings = new Dynamicweb.Search.ModuleSettings() { Database = "DW_samples", ItemTable = "DvkArticle", ItemIDQueryParameterName = "ArticleId", CategoryTable = "Categories", CategoryIdField = "Id", CategoryNameField = "Description", ItemIdField = "Id", ItemCategoryIdField = "CategoryId", ItemNameField = "Title", ItemDateField = "CreateDateTime", ItemLastModifiedDateField = "UpdateDateTime", ItemSearchFields = "Title, Summary, Body", ItemTextField = "Body", ItemActiveFromDateField = "PublicationDateFrom", ItemActiveToDateField = "PublicationDateTo", ItemActiveField = "Deleted", ModuleName = moduleSystemName, SystemName = moduleSystemName // Important or it won't save }, }; module.Save(); } }
Now, in order to register this module, all you need is a call to RegisterAutoRegisterModule.
Caveats
As is often the case with Dynamicweb, there are a few caveats:
Base.IsModuleInstalled and Save rely on HttpContext which may not always be available when you feel like calling this code. I tried this code in Application_Start and it failed because "Request is not available in this context" when running in Integrated mode. I then tried it in Application_BeginRequest where it fails as well as the Save method uses HttpContext.Current.Session which isn't available. Eventually, I ended up calling it in Application_OnPreRequestHandlerExecute which seems to work well. Calling IsModuleInstalled with false for the refreshCache parameter is a pretty cheap operation, so it's not too bad this code gets called too often. if you find it problematic nonetheless, keep track of whether you already registered the module or not yourself, or make it user driven in some Admin page in your site.
A few other things worth mentioning:
- If you new up an instance of SearchSettings as I've done here, the instance does not get its Name and SystemName from its parent RegisterModules.Module instance. This means you must specify Name and SystemName again, or the search settings won't be saved. Alternatively, you can reuse the existing instance (which is created in the property's getter) and assign the values using the dot notation (e.g.: module.SearchSettings.ItemTable = "DvkArticle").
- It seems that RegisterModules.Module.Save() doesn't take the ControlPanel property into account. Looks like a bug to me. Either execute an UPDATE statement against the Module table yourself, or register it manually.
Downloads
Below you can download the full running demo for the Custom Module article series. Note that this is the same code as from part 8, and that I haven't added the code shown in this article.