Creating the module

The module described in this section registers all necessary components to work with testimonials and creates the backend pages that provide an overview of submitted testimonials.

To implement the module class, perform the following:

  1. In the context menu of the Testimonials folder, click Add » Class...

  2. In the Name input field, enter TestimonialsModule.

  3. Open the file TestimonialsModule.cs and add the following namespaces:

    using Telerik.Sitefinity.Abstractions;
    using Telerik.Sitefinity.Configuration;
    using Telerik.Sitefinity.GenericContent.Model;
    using Telerik.Sitefinity.Modules.Pages;
    using Telerik.Sitefinity.Modules.Pages.Configuration;
    using Telerik.Sitefinity.Pages.Model;
    using Telerik.Sitefinity.Services;

  4. Change the class definition to:

    public class TestimonialsModule : ModuleBase
    {
     
    }

  5. Add the following properties:

    public static string ModuleName = "Testimonials";
    public static string UrlName = "Testimonials";
     
    public override Guid LandingPageId
    {
        get { return new Guid("D7CC19E4-BCE6-4D0F-97D0-C2B667DBBAA8"); }
    }
     
    public Guid CreatePageId
    {
        get { return new Guid("A58078E4-9CCA-43C4-BAC7-EAE0EFE0489A"); }
    }
     
    public Guid EditPageId
    {
        get { return new Guid("C1725944-EC74-4F05-8BA2-3DA5B57CE325"); }
    }
     
    public override Type[] Managers
    {
        get { return null; }
    }

  6. Define the following helper method:

    private void CreatePage(PageManager pageManager, Guid pageID, Guid parentPageID, string UrlName, bool ShowInNavigation, string Title, PageControl control)
    {
        // get backend node
        var parentPage = pageManager.GetPageNode(parentPageID);
     
        // Create a node in the SiteMap for that page.
        var node = pageManager.CreatePageNode(pageID);
        pageManager.ChangeParent(node, parentPage);
        parentPage.Nodes.Add(node);
     
        // set page properties
        node.RenderAsLink = true;
        node.Title = Title;
        node.ShowInNavigation = ShowInNavigation;
        node.UrlName = UrlName;
     
        // Create a PageData object to hold the actual page contents
        var pageData = pageManager.CreatePageData();
        pageData.Template = pageManager.GetTemplate(SiteInitializer.DefaultBackendTemplateId);
        pageData.HtmlTitle = Title;
        pageData.Title = Title;
        pageData.Status = ContentLifecycleStatus.Live;
        pageData.Visible = true;
        pageData.Version = 1;
     
        // associate the node with the PageData object
        node.Page = pageData;
     
        // add admin control to the page
        if (control != null) pageData.Controls.Add(control);
    }

  7. Implement the ModuleBase by pasting the following code:

    public override void Upgrade(SiteInitializer initializer, Version upgradeFrom) { }
     
    protected override ConfigSection GetModuleConfig() { return null; }

  8. To install the backend pages and register the controls in the toolbox, add the following method:

    public override void Install(SiteInitializer initializer)
    {
        // get page manager
        var pageManager = initializer.PageManager;
     
        // create Module Landing Page if doesn't exist
        var landingPage = pageManager.GetPageNodes().SingleOrDefault(p => p.Id == this.LandingPageId);
        if (landingPage == null)
        {
            // create admin list view control and add to new landing page
            var ctrl = pageManager.CreateControl<PageControl>("~/Modules/Testimonials/Admin/TestimonialsAdminView.ascx", "Content");
            CreatePage(pageManager, LandingPageId, SiteInitializer.ModulesNodeId, TestimonialsModule.ModuleName, true, TestimonialsModule.ModuleName, ctrl);
        }
     
        // create testimonials "Create" Page if doesn't exist
        var createPage = pageManager.GetPageNodes().SingleOrDefault(p => p.Id == this.CreatePageId);
        if (createPage == null)
        {
            // create admin control, set properties
            var ctrl = pageManager.CreateControl<PageControl>("~/Modules/Testimonials/Admin/TestimonialsAddEditView.ascx", "Content");
            var prop = ctrl.Properties.FirstOrDefault(p => p.Name == "Mode");
            if (prop == null)
            {
                prop = new ControlProperty();
                prop.Id = Guid.NewGuid();
                prop.Name = "Mode";
                ctrl.Properties.Add(prop);
            }
     
            // set control to "Create" mode
            prop.Value = SitefinityWebApp.Modules.Testimonials.Admin.TestimonialsAddEditView.AdminControlMode.Create.ToString();
     
            // create backend page and add control
            CreatePage(pageManager, CreatePageId, LandingPageId, "Create", false, "Create Testimonial", ctrl);
        }
     
        // create testimonials "Edit" Page if doesn't exist
        var editPage = pageManager.GetPageNodes().SingleOrDefault(p => p.Id == this.EditPageId);
        if (editPage == null)
        {
            // create admin control, set properties
            var ctrl = pageManager.CreateControl<PageControl>("~/Modules/Testimonials/Admin/TestimonialsAddEditView.ascx", "Content");
            var prop = ctrl.Properties.FirstOrDefault(p => p.Name == "Mode");
            if (prop == null)
            {
                prop = new ControlProperty();
                prop.Id = Guid.NewGuid();
                prop.Name = "Mode";
                ctrl.Properties.Add(prop);
            }
     
            // set control to "Create" mode
            prop.Value = SitefinityWebApp.Modules.Testimonials.Admin.TestimonialsAddEditView.AdminControlMode.Edit.ToString();
     
            // create backend page and add control
            CreatePage(pageManager, EditPageId, LandingPageId, "Edit", false, "Edit Testimonial", ctrl);
        }
     
        // Install configuration
        // get section from toolbox
        var config = initializer.Context.GetConfig<ToolboxesConfig>();
        var pageControls = config.Toolboxes["PageControls"];
        var section = pageControls
            .Sections
            .Where<ToolboxSection>(e => e.Name == ToolboxesConfig.ContentToolboxSectionName)
            .FirstOrDefault();
     
        // create section it if it doesn't exist
        if (section == null)
        {
            section = new ToolboxSection(pageControls.Sections)
            {
                Name = ToolboxesConfig.ContentToolboxSectionName,
                Title = "ContentToolboxSectionTitle",
                Description = "ContentToolboxSectionDescription",
                ResourceClassId = typeof(PageResources).Name
            };
            pageControls.Sections.Add(section);
        }
     
        // add widget to section if it doesn't exist
        if (!section.Tools.Any<ToolboxItem>(e => e.Name == TestimonialsView.ViewName))
        {
            var tool = new ToolboxItem(section.Tools)
            {
                Name = TestimonialsView.ViewName,
                Title = "Testimonials View",
                Description = "Public control for the Testimonials module",
                ControlType = "~/Modules/Testimonials/TestimonialsView.ascx",
                CssClass = "sfTestimonialsWidget"
            };
            section.Tools.Add(tool);
        }
    }

TestimonialsModule inherits from ModuleBase. Install creates data tables, installs pages and registers the controls in the toolbox.

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