Five New Optimizely Certifications are Here! Validate your expertise and advance your career with our latest certification exams. Click here to find out more

Alexander Haneng
Sep 28, 2012
  25599
(0 votes)

How to define default values for pages and blocks in EPiServer CMS 7

In EPiServer 7 we can define page types and block types in code, but how do you set the default values? Lets find out.

 

Override the SetDefaultValues method

To set default values for page types and block types you need to override the PageData SetDefaultValues method.

 

//Sets the default property values
public override void SetDefaultValues(ContentType contentType)
{
    base.SetDefaultValues(contentType);
 
    //Set up your defaults here 
 
}

 

 

Page type example

We have a page type called ArticleList that have the following properties: SourcePage, MaxNumberOfPages and SortOrder. We want to have the source page default to the start page, the max number of pages to default to 5 and sort order to be alphabetical.

 

using System;
using System.ComponentModel.DataAnnotations;
using EPiServer.Core;
using EPiServer.DataAbstraction;
using EPiServer.DataAnnotations;
using EPiServer.Filters;
using EPiServer.SpecializedProperties;
 
namespace EPiServer.Templates.AlloyTech.PageTypes
{
    [ContentType(DisplayName = "ArticleList")]
    public class ArticleList : PageData
    {
        [Display(
            Name = "List children of the page",
            GroupName = SystemTabNames.Content,
            Order = 2)]
        public virtual PageReference SourcePage { get; set; }
 
 
        [Display(
            Name = "Max number of pages to show",
            GroupName = SystemTabNames.Content,
            Order = 3)]
        public virtual int MaxNumberOfPages { get; set; }
 
 
        [BackingType(typeof(PropertySortOrder))]
        [Display(
            Name = "Sort the pages by",
            GroupName = SystemTabNames.Content,
            Order = 4)]
        public virtual int SortOrder { get; set; }
 
 
        //Sets the default property values
        public override void SetDefaultValues(ContentType contentType)
        {
            base.SetDefaultValues(contentType);
 
            //Set up your defaults here 
            SourcePage = ContentReference.StartPage;
            MaxNumberOfPages = 5;
            SortOrder = (int) FilterSortOrder.Alphabetical;
 
            //You can also set defaults for the built in properties
            VisibleInMenu = false;
            this[MetaDataProperties.PageChildOrderRule] = FilterSortOrder.Index;
            this[MetaDataProperties.PagePeerOrder] = 10; //Sort index
            StopPublish = DateTime.Now.AddDays(30); //Unpublish after 30 days
        }
    }
 
}

 

When the editor creates a new page of the type article list the property defaults will be loaded:

 

image

 

As you can see from the example you can also set defaults for the built in properties like visible in menu, child sort order, sort index and stop publish.

 

//Sets the default property values
public override void SetDefaultValues(ContentType contentType)
{
    base.SetDefaultValues(contentType);
 
    //Set up your defaults here 
    SourcePage = ContentReference.StartPage;
    MaxNumberOfPages = 5;
    SortOrder = (int) FilterSortOrder.Alphabetical;
 
    //You can also set defaults for the built in properties
    VisibleInMenu = false;
    this[MetaDataProperties.PageChildOrderRule] = FilterSortOrder.Index;
    this[MetaDataProperties.PagePeerOrder] = 10; //Sort index
    StopPublish = DateTime.Now.AddDays(30); //Unpublish after 30 days
}

 

The result:

image

 

 

Block type example

You can set defaults for blocks in the same way.

using System;
using System.ComponentModel.DataAnnotations;
using EPiServer.Core;
using EPiServer.DataAbstraction;
using EPiServer.DataAnnotations;
using EPiServer.SpecializedProperties;
 
namespace EPiServer.Templates.AlloyTech.BlockTypes
{
    [ContentType(DisplayName = "Image")]
    public class Image : BlockData
    {
        [Display(
            Name = "Image",
            GroupName = SystemTabNames.Content,
            Order = 1)]
        [BackingType(typeof(PropertyImageUrl))]
        public virtual String ImageUrl { get; set; }
 
        [Display(
            Name = "Alt text",
            GroupName = SystemTabNames.Content,
            Order = 2)]
        public virtual String AltText { get; set; }
 
 
 
        //Sets the default property values
        public override void SetDefaultValues(ContentType contentType)
        {
            base.SetDefaultValues(contentType);
 
            //Set up your defaults here 
            ImageUrl = "/images/blank.png";
            AltText = "Image description";
        }
    }
}

 

What happens if the admin changes the default values in admin mode?

Default values set in admin mode are applied after the SetDefaultValues-method has been called and will thus override your default values defined in code. (Thanks for clearing this up Linus)

Sep 28, 2012

Comments

Please login to comment.
Latest blogs
Optimizely Configured Commerce and Spire CMS - Figuring out Handlers

I recently entered the world of Optimizely Configured Commerce and Spire CMS. Intriguing, interesting and challenging at the same time, especially...

Ritu Madan | Mar 12, 2025

Another console app for calling the Optimizely CMS REST API

Introducing a Spectre.Console.Cli app for exploring an Optimizely SaaS CMS instance and to source code control definitions.

Johan Kronberg | Mar 11, 2025 |

Extending UrlResolver to Generate Lowercase Links in Optimizely CMS 12

When working with Optimizely CMS 12, URL consistency is crucial for SEO and usability. By default, Optimizely does not enforce lowercase URLs, whic...

Santiago Morla | Mar 7, 2025 |

Optimizing Experiences with Optimizely: Custom Audience Criteria for Mobile Visitors

In today’s mobile-first world, delivering personalized experiences to visitors using mobile devices is crucial for maximizing engagement and...

Nenad Nicevski | Mar 5, 2025 |

Unable to view Optimizely Forms submissions when some values are too long

I discovered a form where the form submissions could not be viewed in the Optimizely UI, only downloaded. Learn how to fix the issue.

Tomas Hensrud Gulla | Mar 4, 2025 |

CMS 12 DXP Migrations - Time Zones

When it comes to migrating a project from CMS 11 and .NET Framework on the DXP to CMS 12 and .NET Core one thing you need to be aware of is the...

Scott Reed | Mar 4, 2025