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
Jul 16, 2012
  32843
(0 votes)

How to define properties in EPiServer 7 - A quick reference

This is a quick reference on how to define strongly typed properties in code in EPiServer CMS 7.

 

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.

 

This is the way you define a property in a page type or in a block type. You can define if a property is searchable, required etc. using attributes.

[BackingType(typeof(PropertyLongString))]
[CultureSpecific]
[Searchable]
[Required]
[Editable(true)]
[Display(
    Name = "Heading",
    Description = "",
    GroupName = SystemTabNames.Content,
    Order = 10)]
public virtual string Heading { get; set; }

 

The attribute defaults are very good, so in most cases you can safely omit them. This code will accomplish the same as the code above:

[Display(
    Name = "Heading",
    Description = "",
    GroupName = SystemTabNames.Content,
    Order = 10)]
public virtual string Heading { get; set; }

 

The following attributes are available:

CultureSpecific(bool) Defines if this property should have a different value for each language. If not set: true for strings, other types false.
Searchable Defines if the property value should be searchable. If not set: true for strings, other types false.
Required Defines if a value for this property must be set before being able to save a page of the parent type. If not set: false.

ScaffoldColumnAttribute(bool)

Defines if this property is visible in Edit mode.If not set: true.

Display(
Name=...,
Description=...,
GroupName=...,
Order=...)

The Name, Description, GroupName and Order properties are used to set the EditCaption, HelpText , Tab and FieldOrder respectively.
UIHint Used to select either editor/renderer or both by defining a hint string. You can use EPiServer.Web.UIHint to use hints for known types in the system, for instance UIHint.Image.
Ignore By default all properties are included in a content type, you can use this attribute to tell the system to ignore this property.
BackingType(Type) Defines the PropertyData type used to store values for this property. Must be a type inheriting from PropertyData. The backing type will be auto determined.

 

 

Validation attributes

If you want to restrict what values the editor can enter into a property you can define this in code and it will be enforced by EPiServer in edit mode. Here are some examples:

 

Restrict the length of a PropertyLongString:

[StringLength(20)]
[Display(
    Name = "Heading",
    Description = "",
    GroupName = SystemTabNames.Content,
    Order = 10)]
public virtual string Heading { get; set; }

 

image

The editor will only be able to enter 20 or less characters (HTML maxlength).

 

 

Restrict the value of a PropertyNumber to a range.

[Range(1,100)]
[Display(
    Name = "Max number of results",
    Description = "",
    GroupName = SystemTabNames.Content,
    Order = 10)]
public virtual Int32 MaxResults { get; set; }

 

image

 

 

Restrict the value of a PropertyString with RegExg

[RegularExpression(@"^([A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,9})$"
    , ErrorMessage = "Must be valid email address")]
[Display(
    Name = "Email",
    Description = "",
    GroupName = SystemTabNames.Content,
    Order = 10)]
public virtual string Email { get; set; }
 

image

 

 

Setting default property values

You can set default values for your properties as well as the built in properties by overriding the method SetDefaultValues on the PageData object.

 

 

Example page type with all the property types

The code below defines a new page type called PageWithAllPropertyTypes which contains (almost) all the property types EPiServer supports. For a few properties you also need to define a UIHint to tell EPiServer exactly what you want. For example if you want an image property you need to define the property as a Url and add the attribute [UIHint(UIHint.Image)].

 
using System;
using System.ComponentModel.DataAnnotations;
using EPiServer.Core;
using EPiServer.DataAbstraction;
using EPiServer.DataAnnotations;
using EPiServer.SpecializedProperties;
using EPiServer.Web;
using EPiServer.XForms;
 
namespace EPiServer.Templates.Alloy.Models.Pages
{
    [ContentType(DisplayName = "Page with all property types", 
        Description = "A page with all the property types defined")]
    public class PageWithAllPropertyTypes : PageData
    {
 
        // ------------------------------------------------
        // PropertyLongString (PropertyString)
        // ------------------------------------------------
        [Display(
            Name = "Heading",
            Description = "",
            GroupName = SystemTabNames.Content,
            Order = 10)]
        public virtual string Heading { get; set; }
 
 
        // ------------------------------------------------
        // PropertyXhtmlString
        // ------------------------------------------------
        [Display(
            Name = "Main body",
            Description = "",
            GroupName = SystemTabNames.Content,
            Order = 20)]
        public virtual XhtmlString MainBody { get; set; }
 
 
        // ------------------------------------------------
        // PropertyUrl
        // ------------------------------------------------
        [BackingType(typeof(PropertyUrl))]
        [Display(
            Name = "Link",
            Description = "",
            GroupName = SystemTabNames.Content,
            Order = 30)]
        public virtual Url Link { get; set; }
 
 
        // ------------------------------------------------
        // PropertyUrl (Image)
        // ------------------------------------------------
        [UIHint(UIHint.Image)]
        [Display(
            Name = "Image",
            Description = "",
            GroupName = SystemTabNames.Content,
            Order = 40)]
        public virtual Url Image { get; set; }
 
 
        // ------------------------------------------------
        // PropertyUrl (Document)
        // ------------------------------------------------
        [UIHint(UIHint.Document)]
        [Display(
            Name = "File",
            Description = "",
            GroupName = SystemTabNames.Content,
            Order = 50)]
        public virtual Url File { get; set; }
 
 
        // ------------------------------------------------
        // PropertyPageReference
        // ------------------------------------------------
        [Display(
            Name = "Sitemap page",
            Description = "",
            GroupName = SystemTabNames.Content,
            Order = 55)]
        public virtual PageReference SitemapPageLink { get; set; }
 
 
        // ------------------------------------------------
        // PropertyLinkCollection
        // ------------------------------------------------
        [Display(
            Name = "Links",
            Description = "",
            GroupName = SystemTabNames.Content,
            Order = 60)]
        public virtual LinkItemCollection Links { get; set; }
 
 
        // ------------------------------------------------
        // PropertyContentArea
        // ------------------------------------------------
        [Display(
            Name = "Right block area",
            Description = "",
            GroupName = SystemTabNames.Content,
            Order = 70)]
        public virtual ContentArea RightBlockArea { get; set; }
 
 
        // ------------------------------------------------
        // PropertyBoolean
        // ------------------------------------------------
        [Display(
            Name = "Show teaser",
            Description = "",
            GroupName = SystemTabNames.Content,
            Order = 80)]
        public virtual Boolean ShowTeaser { get; set; }
 
 
        // ------------------------------------------------
        // PropertyCategory
        // ------------------------------------------------
        [Display(
            Name = "Selected categories",
            Description = "",
            GroupName = SystemTabNames.Content,
            Order = 90)]
        public virtual CategoryList SelectedCategories { get; set; }
 
 
        // ------------------------------------------------
        // PropertyDate
        // ------------------------------------------------
        [Display(
            Name = "Event start time",
            Description = "",
            GroupName = SystemTabNames.Content,
            Order = 100)]
        public virtual DateTime EventStartTime { get; set; }
 
 
        // ------------------------------------------------
        // PropertyFloatNumber
        // ------------------------------------------------
        [Display(
            Name = "Price",
            Description = "",
            GroupName = SystemTabNames.Content,
            Order = 110)]
        public virtual Double Price { get; set; }
 
 
        // ------------------------------------------------
        // PropertyNumber
        // ------------------------------------------------
        [Display(
            Name = "Votes",
            Description = "",
            GroupName = SystemTabNames.Content,
            Order = 120)]
        public virtual Int32 Votes { get; set; }
 
 
        // ------------------------------------------------
        // PropertyPageType
        // ------------------------------------------------
        [Display(
            Name = "Filter for page type",
            Description = "",
            GroupName = SystemTabNames.Content,
            Order = 130)]
        public virtual PageType FilterPageType { get; set; }
 
 
        // ------------------------------------------------
        // PropertyXForm
        // ------------------------------------------------
        [Display(
            Name = "Form",
            Description = "",
            GroupName = SystemTabNames.Content,
            Order = 140)]
        public virtual XForm Form { get; set; }
 
 
         // ------------------------------------------------
        // PropertyString
        // Only use if you really have to.
        // Use PropertyLongString instead with [StringLength(255)]
        // ------------------------------------------------
        [BackingType(typeof(PropertyString))]
        [Display(
            Name = "Short intro",
            Description = "",
            GroupName = SystemTabNames.Content,
            Order = 150)]
        public virtual string ShortIntro { get; set; }
 
 
    }
}
 
 

The resulting page type in admin mode:

image

 

 

 

Other posts in this blog series:

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

How to create a simple Block Type in EPiServer CMS 7

Jul 16, 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