Alexander Haneng
Sep 28, 2012
  43400
(9 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

Antti Alasvuo
Antti Alasvuo Sep 28, 2012 11:53 AM

Nice post but I would add here that when setting for example the metadata properties one should use the MetaDataProperties constants. For example : MetaDataProperties.PageChildOrderRule

And also if you want to preserver the possibility to change the default values from the Admin for a page type (overriding code default values), I think you should do it this way:

PageType pt = contentType as PageType;
// pretend we don't know that the current base implementation would thow exception
// if the passed contentType isn't PageType
if (pt != null)
{
// set the sorting only if there are no default values, if there are default values
// someone has set those via admin default values for the pagetype
if (!pt.Defaults.HasValues)
{
this[MetaDataProperties.PageChildOrderRule] = FilterSortOrder.Alphabetical;
}
}

Sep 28, 2012 02:26 PM

Settings defined in admin are applied after the SetDefaultValues-method has been called so you don't have to take this into consideration. That is, settings defined in admin will always override settings from your model class.

Antti Alasvuo
Antti Alasvuo Oct 1, 2012 05:59 AM

Ok, I guess that has been fixed then (because I based that on my experience on a certain version).

Oct 3, 2012 01:27 PM

I have updated the example to use MetaDataProperties.

Vladimir Levchuk
Vladimir Levchuk Apr 9, 2013 05:57 PM

Hi,

What is the correct way to define default values for existing page types? I.e. I'm adding a new property to the existing page type and want to provide the default value for one.

May 2, 2013 09:36 AM

Do I have to do anything special to make this work? Because for me the function isn't even called. I do inherit from BlockData.

määäts
määäts May 8, 2013 09:47 AM

imo it would have been neater to be able to use the System.Component.DefaultValue attribute instead of the SetDefaultValue override approach.

Alexander Helsinghof
Alexander Helsinghof Sep 9, 2013 10:51 AM

Great post!
Just wondering:
If you want to set a maximum and minimum value for the dropdownlist as int32.
How do you set it?

By that I mean; The Editor needs to be able to choose how many pages to show, wich isnt a problem but right now they can pick 100 if they want. I want maximum 10.
Thanks
Alexander

Alexander Helsinghof
Alexander Helsinghof Sep 9, 2013 10:54 AM

Ofcourse I didn't try the [Range 1,6] Wich is perfect for the problem!

Birger Hedberg-Olsson
Birger Hedberg-Olsson Oct 1, 2013 03:09 PM

Can I define a default block for a content area?

If so, how do I go about doing this?

nitin anand
nitin anand Jan 23, 2019 10:41 AM

same question as Vladimir Levchuk

Tim Schmelter
Tim Schmelter Jul 10, 2020 03:10 PM

same question as Vladimir Levchuk

Please login to comment.
Latest blogs
Block type selection doesn't work

Imagine you're trying to create a new block in a specific content area. You click the "Create" link, expecting to see a CMS modal with a list of...

Damian Smutek | Nov 4, 2024 | Syndicated blog

.NET 8 FAQ

I have previously written about .NET compatibility in general and .NET 8 in particular, see blog posts here , here and here . With the end of suppo...

Magnus Rahl | Nov 4, 2024

Dynamic packages in Commerce Connect

In Optimizely Commerce Connect, you can group different items using packages and bundles. Package: A package has one or more versions of a product...

K Khan | Nov 1, 2024

Efficient Catalog Metadata Management and Product Updates Using DTOs in Optimizely Commerce

This post explores ways to manage and update catalog metadata in Optimizely Commerce by utilizing Data Transfer Objects (DTOs). DTOs provide a...

Sujit Senapati | Oct 31, 2024

Effortlessly Resize Images with Cloudflare's On-the-Fly Solution

Resizing images in C# has traditionally been a complex and time-consuming task, often requiring intricate code and handling various image processin...

Manoj Kumawat | Oct 31, 2024 | Syndicated blog

XSS Vulnerabilities Patched with TinyMCE 6.8.4

Two different XSS vulnerabilities were fixed in the latest update of the NuGet package EPiServer.CMS.TinyMce. Update today!

Tomas Hensrud Gulla | Oct 30, 2024 | Syndicated blog