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(
|
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
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.
FYI,
In Release7 RenderDescriptor has been renamed with TemplateDescriptor
When creating a new type, where do you get the ContentType attribute GUID? Do I just generate one myself?