A critical vulnerability was discovered in React Server Components (Next.js). Our systems remain protected but we advise to update packages to newest version. Learn More

Alexander Haneng
Aug 8, 2012
  11445
(0 votes)

How to create a Page Type in code for EPiServer CMS 7

In EPiServer 7 CMS you can defined page types in code. In this blog post we cover how to create your first page type in this way.

 

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.

 

A simple page type

A page type in EPiServer 7 CMS consists of two parts:

1. A page type class (.cs) that defines what properties the page should contain

2. A page template (.aspx) that renders the page and those properties.

 

Creating a page type class

Here is a simple page type class that defines our page type MyPage:

 

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; }
    }
}
 

MyPage has a single string property called MyName.

Here is a quick reference on how to define the other property types in EPiServer 7, like XhtmlString, LinkCollection and Number

 

Creating a page template

We now need a page template that can render pages of our page type MyPage.

 

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 style="background-color: white;">
<form id="Form1" runat="server">
<div>
<h1>MyPage Template</h1>    
My name is
<EPiServer:Property runat="server" PropertyName="MyName" />
</div>
</form>
</body>
</html>

 

In the code behind for the aspx file we set the connection between the page type class and the page template by setting TemplatePage<MyPage> (where MyPage is our page type class.)

 

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>
    {
    }
}

 

Using our new page type

After building the project and starting our EPiServer 7 site again we will see the new page type in admin mode:

image

 

The page type is also available in edit mode when we click create a new page:

image

(If you are using the Alloy demo and can’t see the MyPage as a page type when you create a new page, try creating a new page under the “Alloy Track” page instead as the start page has some restrictions set)

 

Lets create a new page of the type MyPage and enter “Alexander” into the MyName property:

image

 

After we publish the page the visitors will see this page:

image

 

That was how to create your first page type in EPiServer 7 from code. The next step is to create a simple block type.

Aug 08, 2012

Comments

Please login to comment.
Latest blogs
A day in the life of an Optimizely OMVP: Learning Optimizely Just Got Easier: Introducing the Optimizely Learning Centre

On the back of my last post about the Opti Graph Learning Centre, I am now happy to announce a revamped interactive learning platform that makes...

Graham Carr | Jan 31, 2026

Scheduled job for deleting content types and all related content

In my previous blog post which was about getting an overview of your sites content https://world.optimizely.com/blogs/Per-Nergard/Dates/2026/1/sche...

Per Nergård (MVP) | Jan 30, 2026

Working With Applications in Optimizely CMS 13

💡 Note:  The following content has been written based on Optimizely CMS 13 Preview 2 and may not accurately reflect the final release version. As...

Mark Stott | Jan 30, 2026

Experimentation at Speed Using Optimizely Opal and Web Experimentation

If you are working in experimentation, you will know that speed matters. The quicker you can go from idea to implementation, the faster you can...

Minesh Shah (Netcel) | Jan 30, 2026

How to run Optimizely CMS on VS Code Dev Containers

VS Code Dev Containers is an extension that allows you to use a Docker container as a full-featured development environment. Instead of installing...

Daniel Halse | Jan 30, 2026

A day in the life of an Optimizely OMVP: Introducing Optimizely Graph Learning Centre Beta: Master GraphQL for Content Delivery

GraphQL is transforming how developers query and deliver content from Optimizely CMS. But let's be honest—there's a learning curve. Between...

Graham Carr | Jan 30, 2026