Try our conversational search powered by Generative AI!

Per Magne Skuseth
Mar 10, 2014
  12944
(4 votes)

Attribute-based default values

Default property values on content in EPiServer are set by overriding SetDefaultValues. In an attempt to make my model classes a bit tidier, by not having to do the override in lots of places, I wrote a little piece of code which enables the use of the DefaultValue attribute (found in System.Component), like so:

[DefaultValue("It's all in the hips")]
public virtual string Tip { get; set; }

 

Below is the code that is needed in order to make this work. In this particular case, SitePageData is the base class for all my page types.

public abstract class SitePageData : PageData
{
    public override void SetDefaultValues(ContentType contentType)
    {
        PropertyInfo[] properties = GetType().BaseType.GetProperties();
        foreach (var property in properties)
        {
            var defaultValueAttribute = property.GetAttribute<DefaultValueAttribute>();
            if (defaultValueAttribute != null)
            {
                this[property.Name] = defaultValueAttribute.Value;
            }
        }
        base.SetDefaultValues(contentType);
    }
}

 

Now, as long as my page types inherits SitePageData, the attribute can be utilized. Example:

[ContentType(DisplayName = "Article", GUID = "ED1B0CD5-1307-49FA-853F-ADF46CCEE1CE")]
public class ArticlePage : SitePageData
{
    [DefaultValue(true)]
    public virtual bool HideSomething { get; set; }
 
    [DefaultValue(10)]
    public virtual int MaxNewsItems { get; set; }
 
    [DefaultValue("Bla bla bla")]
    public virtual XhtmlString MainBody { get; set; }
}
Mar 10, 2014

Comments

Mar 10, 2014 10:08 AM

Nice!

Vincent
Vincent Mar 10, 2014 11:12 AM

IMO, I prefer to set these values in one place as you override the SetDefaultValues function, it applies to the new page instance only once. If you make them as attribute, it will mislead the other developers as they may try to use this to alter the property's value.

Mar 10, 2014 11:47 AM

Simple and good idea! Thanx!

valdis
valdis Mar 10, 2014 01:58 PM

Nice!

Gayathri Saravanan
Gayathri Saravanan Mar 13, 2014 05:01 PM

Nice...

Per Magne Skuseth
Per Magne Skuseth May 12, 2014 04:29 PM

Thanks! However, Code Monkey does have a really good point, which should be seriously considered before thinking about implementing something like this.

Henrik Fransas
Henrik Fransas Jun 22, 2015 02:53 PM

Have not seen this before, Great work as always Per Magne!

Shoma Gujjar
Shoma Gujjar Jul 29, 2015 02:07 PM

Hi,

Thanks for the great post. It works well however, if i make my property [Required] , it does not apply the default attribute. Is this an issue?

nitin anand
nitin anand Jan 23, 2019 12:20 PM

Any way to set default values to new properties on existing pages?

Please login to comment.
Latest blogs
From Procrastination to Proficiency: Navigating Your Journey to Web Experimentation Certification

Hey there, Optimizely enthusiasts!   Join me in celebrating a milestone – I'm officially a certified web experimentation expert! It's an exhilarati...

Silvio Pacitto | May 17, 2024

GPT-4o Now Available for Optimizely via the AI-Assistant plugin!

I am excited to announce that GPT-4o is now available for Optimizely users through the Epicweb AI-Assistant integration. This means you can leverag...

Luc Gosso (MVP) | May 17, 2024 | Syndicated blog

The downside of being too fast

Today when I was tracking down some changes, I came across this commit comment Who wrote this? Me, almost 5 years ago. I did have a chuckle in my...

Quan Mai | May 17, 2024 | Syndicated blog

Optimizely Forms: Safeguarding Your Data

With the rise of cyber threats and privacy concerns, safeguarding sensitive information has become a top priority for businesses across all...

K Khan | May 16, 2024

The Experimentation Process

This blog is part of the series -   Unlocking the Power of Experimentation: A Marketer's Insight. Welcome back, to another insightful journey into...

Holly Quilter | May 16, 2024

Azure AI Language – Sentiment Analysis in Optimizely CMS

In the following article, I showcase how sentiment analysis, which is part of the Azure AI Language service, can be used to detect the sentiment of...

Anil Patel | May 15, 2024 | Syndicated blog