Archive

Archive for the ‘C#’ Category

Create Composer page type and content function using PageTypeBuilder 1.2

June 24, 2010 14 comments

Introduction:

Page Type Builder (a.k.a PTB) by Joel Abrahamsson is one of the most popular Open Source projects for EPiServer CMS developers. It helps increase developer productivity when creating EPiServer solutions. See http://pagetypebuilder.codeplex.com/ for information and links.

EPiServer Composer: Using a very few main templates combined with independent inner templates and functions, you can use EPiServer Composer to create custom pages with features not available before. EPiServer Composer  gives you notably more flexibility than the standard version of EPiServer CMS.

http://world.episerver.com/Download/Items/Extension-3/EPiServer-Composer-4-Beta/

I take a look into  new version of PTB is released (PageTypeBuilder 1.2) and tried to create Composer page type and content function by using PTB. Everything work well together. As many of you might know that in previous version, we have some issues in using Page Type Builder and Composer though (Stefan Forsberg pointing this out). In latest version, Joel Abrahamsson was made changes in API to check null in DataFactory_LoadedPage to play nicer with Composer.

In this sample, I just want to show how to create new Composer page type and Content function by using Page Type Builder. Beside that there is a trick to put Composer property in existing Tab of EPiServer CMS. You can refer to PageTypeBuilder home page in CodePlex on how to create new page type using Page Type Builder (http://world.episerver.com/en/Articles/Items/Introducing-Page-Type-Builder/)

1. Create new page type

using Dropit.Extension.SpecializedProperties;

using PageTypeBuilder;

namespace EPiServer.Templates.Demo.Pages

{

[PageType(Filename = "~/Templates/Demo/Pages/TestPage.aspx", Name = "Test page type", AvailableInEditMode = true, Description = "Test page type")]

public class TestPageType : TypedPageData

{

[PageTypeProperty(Type = typeof(ExtensionContentAreaProperty), DisplayInEditMode = false, Searchable = true, UniqueValuePerLanguage = false, Tab = typeof(ComposerTab))]

public virtual string MainArea { get; set; }

[PageTypeProperty(Type = typeof(ExtensionPageProperty), DisplayInEditMode = true, Searchable = true, Tab = typeof(ComposerTab))]

public virtual string ExtensionPageProperty { get; set; }

}

}

Create new page type by using TestPage.aspx file as template page. Most important things is the required properties of Composer page type ExtensionPageProperty.

In the sample, I create a page type that look the same as Composer wide page (in Composer sample package) with only one content area. Here you can see that I used ExtensionContentAreaProperty for MainArea and ExtensionPageProperty. The Tab agrument was assigned to ComposerTab, I will explain about this later in this post.

– The template file look like that:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TestPage.aspx.cs" Inherits="EPiServer.Templates.Demo.Pages.TestPage" MasterPageFile="~/Templates/Demo/MasterPages/MasterPage.master" %>

<asp:Content ID="Content1" ContentPlaceHolderID="MainRegion" runat="server">

<!–

The page contains only one single Extension content area: MainArea.

Using divider functions for designing the layout

–>

<div id="StageArea" class="StartPage" style="min-height:100px;">

<Extension:ExtensionContentArea runat="server" ID="MainArea" Description="MainArea using all available space" />

</div>

</asp:Content>

And the code behind:

using System;

using Dropit.Extension.Core;

using PageTypeBuilder.UI;

 

namespace EPiServer.Templates.Demo.Pages

{

    public partial class TestPage : TemplatePage<TestPageType>

    {

        //Add a ExtensionPageHandler for enable Composer

        private readonly ExtensionPageHandler _extensionHandler;

 

        public TestPage()

        {

            _extensionHandler = new ExtensionPageHandler();

        }

 

        protected override void OnInit(EventArgs e)

        {

            base.OnInit(e);

            //It’s important that _extensionHandler initialized after base.OnInit.

            _extensionHandler.Initialize(this);

        }

 

        protected void Page_Load(object sender, EventArgs e)

        {

 

        }

    }

}

2. Create new content function

using Dropit.Extension.SpecializedProperties;

using PageTypeBuilder;

 

namespace EPiServer.Templates.Demo.Pages

{

    [PageType(Filename = "/composer/functions/text.ascx", AvailableInEditMode = true, Name = "Test Article Function", Description = "Test Article Function")]

    public class TestContentFunction : TypedPageData

    {

        [PageTypeProperty(Type = typeof(ExtensionFunctionProperty), DisplayInEditMode = false, Searchable = false, UniqueValuePerLanguage = true, Tab = typeof(ComposerTab))]

        public virtual string ExtensionContentFunctionProperty { get; set; }

        [PageTypeProperty(UniqueValuePerLanguage = false, Searchable = false, DisplayInEditMode = false, Tab = typeof(ComposerTab))]

        public virtual bool NeverUsedProperty { get; set; }

        [PageTypeProperty(UniqueValuePerLanguage = false, Searchable = true, DisplayInEditMode = true, Tab = typeof(ComposerTab), EditCaption = "Heading text")]

        public virtual string Heading { get; set; }

        [PageTypeProperty(UniqueValuePerLanguage = false, Searchable = true, DisplayInEditMode = true, Tab = typeof(ComposerTab), EditCaption = "Body text")]

        public virtual string MainBody { get; set; }

    }

}

Here I use the same template file of Article content function from Composer sample package. Note that we define required properties from Composer and its type.

3. Put page type properties into an existed Tab in EPiServer CMS

Create new class inherited from PageTypeBuilder.Tab. Find the tab that you want to put your properties (Edit Tabs section under Page Type of EPiServer CMS Admin mode)

image

In this sample, I will put all properties to Extension tab.

using EPiServer.Security;

using PageTypeBuilder;

 

namespace EPiServer.Templates.Demo.Pages

{

    public class ComposerTab : Tab

    {

        #region Overrides of Tab

 

        public override string Name

        {

            get { return "Extension"; }

        }

 

        public override AccessLevel RequiredAccess

        {

            get { return AccessLevel.Edit; }

        }

 

        public override int SortIndex

        {

            get { return 1000; }

        }

 

        #endregion

    }

}

Must return exactly the value of Extension Tab (Name, Required Access Level and the Sort index). If we don’t return the same value of Extension tab, then PageTypeBuilder will create new one.

After all, compile the project and go to Admin mode of EPiServer CMS. You can see new page type for Composer page type and content function appeared. New page type and content function also automatically registered in Composer Admin mode. Now you can using new page type and new content function in Edit mode and View mode.

Enjoy Composer and Page Type Builder!

Advertisements