Try our conversational search powered by Generative AI!

Set Defaults Values to a new property added after create the block type

Vote:
 

Hello All,

I'm wondering how I can set the default value of a new property that I added to an existent block. I know that I can override the method "SetDefaultValues" but this will only work when the editor create a new block.

What a about the block that were already created?

In the code Bellow "NewProperty" is the property added to the block "MyExistenBlock" and I want to give a default value "Hello World" even for the blocks already created in the BE of Episerver by the editors

public class MyExistentBlock : BlockData 

{

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

  

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

public override void SetDefaultValues(ContentType contentType)
        {
            base.SetDefaultValues(contentType);
 
            //Set up your defaults here 
            NewProperty = "Hello world";
        }


}
#175855
Mar 04, 2017 11:19
Vote:
 

Hello Juan,

Technically you could define NewProperty like this:

[Display(GroupName = SystemTabNames.Content)]
public virtual string NewProperty
{
    get { return this.GetPropertyValue(page => page.NewProperty) ?? "Hello world"; }
    set { this.SetPropertyValue(page => page.NewProperty, value); }
}

But then "Hello world" will be a fallback value, i.e. NewProperty will never be null.

In All Properties view, however, the text box will be empty if an editor did not specify the value.

#175856
Edited, Mar 04, 2017 13:33
Vote:
 

Thank you @Dejan Caric

We don't have a way to not be empty in the "All Properties view"?

#175857
Mar 04, 2017 14:04
Vote:
 

In that case I would hook up to the ContentSaving or ContentPublishing event and set the value. Keep in mind to describe this functionality for the editors as they might be confused in the beginning.

http://jondjones.com/how-to-hook-into-the-episerver-cms-events-pipeline/

#175881
Mar 05, 2017 19:46
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.