London Dev Meetup Rescheduled! Due to unavoidable reasons, the event has been moved to 21st May. Speakers remain the same—any changes will be communicated. Seats are limited—register here to secure your spot!

Alexander Haneng
Aug 30, 2012
  14872
(0 votes)

How to define Page Types in EPiServer 7 CMS - A quick reference

This blog post is a quick reference on how to define page types in code in EPiServer 7 CMS.

 

Updated to EPiServer 7 CMS on 14.01.2013

This blog post was originally written for a preview version of EPiServer 7 CMS, but is now updated to the final release version.

 

Creating a new page type

To create a new page type you need two things:
1. a page type class (.cs)
2. a page template (.aspx)

If you are new to creating EPiServer 7 page types start by reading How to create a simple Page Type in code for EPiServer CMS 7 first.

 

 

The page type class

The first part of a new page type is creating a page type class(.cs)  that inherits from PageData

 

MyPage.cs

using System.ComponentModel.DataAnnotations;
using EPiServer.Core;
using EPiServer.DataAbstraction;
using EPiServer.DataAnnotations;
 
namespace EPiServer.Templates.Alloy.Models.Pages
{
    [ContentType(DisplayName = "MyPage")]
    public class MyPage : PageData
    {
        [Display(
            Name = "My name",
            Description = "",
            GroupName = SystemTabNames.Content,
            Order = 1)]
        public virtual string MyName { get; set; }
    }
}

 

 

The page template page

To display your page type you need a template page (.aspx).

 

MyPageTemplate.aspx

<%@ Page Language="c#"
    Inherits="EPiServer.Templates.Alloy.Views.Pages.MyPageTemplate" 
    CodeBehind="MyPageTemplate.aspx.cs" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>My Page Template</title>
</head>
<body>
<form id="Form1" runat="server">
<div>
<EPiServer:Property runat="server" PropertyName="MyName" />
</div>
</form>
</body>
</html>

 

You need to link the page type class to the template in the code behind file (.aspx.cs) by referencing the right class (MyPage).

 

MyPageTemplate.aspx.cs

using EPiServer.Framework.DataAnnotations;
using EPiServer.Templates.Alloy.Models.Pages;
 
namespace EPiServer.Templates.Alloy.Views.Pages
{
    [TemplateDescriptor(Path = "~/Views/Pages/MyPageTemplate.aspx")]
    public partial class MyPageTemplate : EPiServer.TemplatePage<MyPage>
    {
    }
}

 

 

 

Page Type Properties

This is the code needed to define a string property on the page type:

[Display(
    Name = "My name",
    Description = "",
    GroupName = SystemTabNames.Content,
    Order = 1)]
public virtual string MyName { get; set; }

 

See the EPiServer 7 property quick reference on how to define other property types

 

 

Adding a local Block to a Page Type

Adding a block to a page type is similar to adding a property. If you have the block MyBlock you add it to the page type like this:

[Display(
    Name = "Contact person",
    Description = "",
    GroupName = SystemTabNames.Content,
    Order = 2)]
public virtual MyBlock ContactPerson { get; set; }

 

You also need to add the block to the page template:

<EPiServer:Property runat="server" PropertyName="ContactPerson" />

 

How to create a simple block type in EPiServer 7

 

 

Adding a Content Area for Shared Blocks

If you want the page type to have an area where the editor can add “shared blocks” you need a ContentArea property:

[Display(
    Name = "Right block area",
    Description = "",
    GroupName = SystemTabNames.Content,
    Order = 3)]
public virtual ContentArea RightBlockArea { get; set; }

 

You also need to add the content area to the page template:

<EPiServer:Property PropertyName="RightBlockArea" runat="server" />

 

 

ContentType Attribute

The available ContentType attributes are:

Property name: Description: Default:
AvailableInEditMode Can new pages be of this type be created by the editors true
Description Text description to show in edit mode null
DisplayName The name in edit mode for the page type null
Order Sort index 100
GUID Unique identifier Guid.Empty
Group name Name for grouping page types null

 

[ContentType(
    DisplayName = "MyPage", 
    Description = "",
    GUID = "7E94EB59-1F75-4502-B3A9-ADB6BD671CFF",
    AvailableInEditMode = true,
    Order = 1,
    GroupName = "AlloyTech")]
public class MyPage : PageData

 

 

Access Attribute

Defines who has access to create pages of this type.

Attribute name: Description: Default:

Access(
Users=...,
Roles=...,
VisitorGroups=...)

Defines who has access to create a new page of this type. Role Everyone

 

[Access(Users = "haneng", Roles="CmsEditors")]
   public class MyPage : PageData

 

 

ImageUrl Attribute

The attribute ImageUrl can be used to set the icon for a page type.

[ImageUrl("~/Templates/AlloyTech/Images/StartPageTypeIcon.png")]
public class StartPage : AlloyPage

 

image

 

 

 

AvailablePageTypes Attribute

You can define in code where the new page type can be created using the AvailablePageTypes attribute.

 

Property name: Description: Default:
Availability Defines if all or none page types should be available. If none is set other settings on the attribute is ignored. Availablity.All
Include A type array of typed pages to specify which page types that should be available under a page instance of the type with the attribute. Type[0]
Exclude A type array of typed pages to specify which page types that should not be available under a page instance of the type with the attribute. Type[0]
IncludeOn States that the page with this attribute should be available under the all the typed pages in the type array. Type[0]
ExcludeOn States that the page with this attribute should be not available under the any of the typed pages in the type array. Type[0]

IncludedOn differs from Include in the way that it is not excluding. That is for types in IncludedOn that has all page types available no page types will be excluded. Include on the other hand will exclude all typed pages except the ones given in Include.

 

[AvailablePageTypes(Include = 
    new Type[] {typeof(StandardPage), typeof(StartPage)})]
public class MyPage : PageData

 

 

 

TemplateDescriptor Attribute

The [TemplateDescriptor] attribute can be used on templates to add meta data to the template. The attribute can also be used to set the template as the default template for the page data.

Property Name: Description: Default:
Path The path to the template to be rendered. Needs to be set if folder structure does not follow namespace structure. null
ModelType The page data type. This can be set on an untyped template which derives from EPiServer.TemplatePage to set the page data type, which will be the type of the CurrentPage. null
Default Defines the template as the default template for this page type. false
Description Description of the template. null
Inherited Inherited means that when this property is set to true, all page data types, which inherits from the ModelType type/Generic type will get the template as a supported template. false

 

[TemplateDescriptor(
    Path = "~/Templates/AlloyTech/Pages/MyPageTemplate.aspx", 
    ModelType = typeof(StandardPage), 
    Default = true, 
    Description = "", 
    Inherited = true
    )]
public partial class MyPageTemplate : EPiServer.TemplatePage<MyPage>

 

 

The EPiServer SDK

A lot of this information is from the excellent EPiServer SDK.

Aug 30, 2012

Comments

Please login to comment.
Latest blogs
Content Compliance Without the Chaos: How Optimizely CMP Empowers Financial Services Marketers

In financial services, content isn’t just about telling your story — it’s about telling it right. Every blog post, product update, or social post i...

abritt | May 22, 2025 |

Opal – Optimizely’s AI-Powered Marketing Assistant

Overview Opal is Optimizely’s AI assistant designed to accelerate and enhance the entire marketing workflow. Integrated natively across...

abritt | May 22, 2025 |

Integrating Address Validation in Optimizely Using Smarty

Address validation is a crucial component of any ecommerce platform. It ensures accurate customer data, reduces shipping errors, and improves the...

PuneetGarg | May 21, 2025

The London Dev Meetup is TOMORROW!!

The rescheduled London Dev Meetup is happening tomorrow, Wednesday, 21st May, at 6pm! This meetup will be Candyspace 's first, and the first one he...

Gavin_M | May 20, 2025

From Agentic Theory to Practicality: Using Optimizely Opal’s Instructions Feature

A practical look at Optimizely Opal’s Instructions feature — from built-in agents to creating and managing custom instruction workflows. Ideal for...

Andy Blyth | May 19, 2025 |

Common Mistakes in Headless Projects with Optimizely

Adopting a headless architecture with Optimizely is a major shift from the traditional MVC-based development that has been the standard for years....

Szymon Uryga | May 19, 2025