Implementing the module class

The module class wraps the entire functionality created for the module. To create the module class, you must perform the following:

  1. From the context menu of the project LocationsModule, click Add » Class.
  2. Name the class file LocationsModule.cs and click Add.
  3. Open the file LocationsModule.cs.
  4. Add the following using statements:

    • using Telerik.Sitefinity.Modules.GenericContent.Configuration;
    • using Telerik.Sitefinity.Configuration;
    • using Telerik.Sitefinity.Abstractions;
    • using LocationsModule.Web.Services;
    • using Telerik.Sitefinity.Services;
    • using LocationsModule.Model;
    • using Telerik.Sitefinity.Modules.Pages.Configuration;
    • using Telerik.Sitefinity.Web.UI.ContentUI;
    • using LocationsModule.Web.UI;
    • using Telerik.Sitefinity.Pages.Model;
    • using LocationsModule.Data;
    • using Telerik.Sitefinity.Modules.Pages;
    • using LocationsModule.Web.UI.Public;
    • using Telerik.Sitefinity.Abstractions.VirtualPath.Configuration;
  5. Make the LocationsModule class inherit the ContentModuleBase class:

    public class LocationsModule : ContentModuleBase
    {
    }

  6. Define the ModuleName constant in the following way:

    public const string ModuleName = "Locations";

  7. Override the Initialize method of the ContentModuleBase class.

    In it you must register the configuration class for the module and the backend service. Following is the code for the method.

    public override void Initialize(ModuleSettings settings)
    {
        base.Initialize(settings);
     
        // initialize configuration file
        Config.RegisterSection<LocationsConfig>();
     
        // register web services
        ObjectFactory.RegisterWebService(typeof(LocationsBackendService), "Sitefinity/Services/Content/Locations.svc");
    }

  8. Override the Install method of the ContentModuleBase class.

    The method installs the module in Sitefinity system.

    public override void Install(SiteInitializer initializer)
    {
        base.Install(initializer);
     
        IModule locationsModule;
        SystemManager.ApplicationModules.TryGetValue(LocationsModule.ModuleName, out locationsModule);
     
        initializer.Context.SaveMetaData(true);
        this.InstallCustomVirtualPaths(initializer);
    }
     
    private void InstallCustomVirtualPaths(SiteInitializer initializer)
    {
        var virtualPathConfig = initializer.Context.GetConfig<VirtualPathSettingsConfig>();
        var productsModuleVirtualPathConfig = new VirtualPathElement(virtualPathConfig.VirtualPaths)
        {
            VirtualPath = "~/Locations/*",
            ResolverName = "EmbeddedResourceResolver",
            ResourceLocation = "LocationsModule"
        };
        if (!virtualPathConfig.VirtualPaths.ContainsKey("~/Locations/*"))
            virtualPathConfig.VirtualPaths.Add(productsModuleVirtualPathConfig);
    }

    In the InstallCustomVirtualPaths method you must register the prefixes used by the LayoutTemplatePath properties of your frontend controls.

  9. Define the LocationsPageGroupID and the LocationsModuleLandingPage constants in the following way:

    public static readonly Guid LocationsPageGroupID = new Guid("000262BF-E8EA-4BE3-8C67-E1C2486A57BE");
    public static readonly Guid LocationsModuleLandingPage = new Guid("7A0F43CE-064A-4E09-A3B9-59CA2E1640A6");

    You will use these constants in the implementation of the InstallPages method.

  10. Implement the abstract members of the ContentModuleBase class in the following way:

    protected override void InstallPages(SiteInitializer initializer)
    {
        // code to install admin page nodes and pages
        // get pagemanager
        var pageManager = initializer.PageManager;
        var modulesPageNode = pageManager.GetPageNode(SiteInitializer.ModulesNodeId);
     
        // need local variable for ID due to limitation in API
        var id = LocationsPageGroupID;
     
        // Create PageNode if doesn't exist
        var locationsModulePageGroupNode = pageManager.GetPageNodes().Where(t => t.Id == id).SingleOrDefault();
        if (locationsModulePageGroupNode == null)
        {
            // create page node under Modules node
            locationsModulePageGroupNode = initializer.CreatePageNode(id, modulesPageNode, NodeType.Group);
            locationsModulePageGroupNode.Name = LocationsModule.ModuleName;
            locationsModulePageGroupNode.ShowInNavigation = true;
            locationsModulePageGroupNode.Attributes["ModuleName"] = LocationsModule.ModuleName;
     
            locationsModulePageGroupNode.Title = "Locations";
            locationsModulePageGroupNode.UrlName = "Locations";
            locationsModulePageGroupNode.Description = "Module for managing a list of Locations";
        }
     
        // local version of landing page ID
        id = this.LandingPageId;
     
        // create Landing Page if doesn't exist
        var landingPage = pageManager.GetPageNodes().SingleOrDefault(p => p.Id == id);
        if (landingPage == null)
        {
            // create page
            var pageInfo = new PageDataElement()
            {
                PageId = id,
                IncludeScriptManager = true,
                ShowInNavigation = false,
                EnableViewState = false,
                TemplateName = SiteInitializer.BackendTemplateName,
     
                Name = LocationsModule.ModuleName,
                MenuName = "Locations Module",
                UrlName = "Locations",
                Description = "Landing page for the Locations Module",
                HtmlTitle = "Locations Module"
            };
     
            pageInfo.Parameters["ModuleName"] = LocationsModule.ModuleName;
     
            // create control panel
            var controlPanel = new BackendContentView()
            {
                ModuleName = LocationsModule.ModuleName,
                ControlDefinitionName = LocationsDefinitions.BackendDefinitionName
            };
     
            // add page
            initializer.CreatePageFromConfiguration(pageInfo, locationsModulePageGroupNode, controlPanel);
        }
    }
     
    public override void Upgrade(SiteInitializer initializer, Version upgradeFrom)
    {
     
    }
     
    protected override void InstallTaxonomies(SiteInitializer initializer)
    {
        this.InstallTaxonomy(initializer, typeof(LocationItem));
    }
     
    protected override ConfigSection GetModuleConfig()
    {
        // code to return Module configuration
        return Config.Get<LocationsConfig>();
    }
     
    protected override void InstallConfiguration(SiteInitializer initializer)
    {
        var config = initializer.Context.GetConfig<ToolboxesConfig>();
     
        var pageControls = config.Toolboxes["PageControls"];
     
        var section = pageControls
            .Sections
            .Where<ToolboxSection>(e => e.Name == ToolboxesConfig.ContentToolboxSectionName)
            .FirstOrDefault();
     
        if (section == null)
        {
            section = new ToolboxSection(pageControls.Sections)
            {
                Name = ToolboxesConfig.ContentToolboxSectionName,
                Title = "ContentToolboxSectionTitle",
                Description = "ContentToolboxSectionDescription",
                ResourceClassId = typeof(PageResources).Name
            };
            pageControls.Sections.Add(section);
        }
        if (!section.Tools.Any<ToolboxItem>(e => e.Name == "LocationsView"))
        {
            var tool = new ToolboxItem(section.Tools)
            {
                Name = "LocationsView",
                Title = "LocationsViewTitle",
                Description = "LocationsViewDescription",
                ModuleName = LocationsModule.ModuleName,
                ControlType = typeof(LocationsView).AssemblyQualifiedName
            };
            section.Tools.Add(tool);
        }
    }
     
    public override Guid LandingPageId
    {
        get { return LocationsModuleLandingPage; }
    }
     
    public override Type[] Managers
    {
        get { return new[] { typeof(LocationsManager) }; }
    }

Related topics:

Feedback

How useful is this article?

Tell us more

Submit
Your message was successfully sent.

We appreciate your feedback.

Your message could not be sent.

OK