Hello Martin
Have you tried using the SetDefaultValues method which is the recommended way of setting default values?
David
Yes, following is the overriden method. It's working for some fields. Any ideas?
public override void SetDefaultValues(ContentType contentType) { var memberInfo = GetType().BaseType; if (memberInfo != null) { var properties = memberInfo.GetProperties(); foreach (var property in properties) { var defaultValueAttribute = property.GetAttribute<DefaultValueAttribute>(); if (defaultValueAttribute != null) { this[property.Name] = defaultValueAttribute.Value; } } } base.SetDefaultValues(contentType); }
Maybe its a code style thing but I always implement the base implementation before my own when overriding methods. Its a guess but perhaps something in the base.SetDefaultValues method may be doing something to override your implementation? Have you been able to debug and actually see the value getting set?
Strangely it is working when creating new pages. Unfortunately, when going under EPI Server / Admin / Content Type / Edit Safezone property of my Block the default value doesn't appear.
As I understand it, the DefaultValue attribute and the SetDefaultValues method only applies values when first creating the content. It's not intended for setting values on existing content when their content types get updated or ensuring that already created content gets updated.
An option is to tap into the OnSaving event and set the values from there if needed.
Something like this:
[ModuleDependency()] public class ContentEvents : IInitializableModule { public void Initialize(InitializationEngine context) { var contentEvents = context.Locate.ContentEvents(); contentEvents.SavingContent += SetDefaultValues; } public void SetDefaultValues(object sender, ContentEventArgs e) { // Set your defaults here if they're not set. var startPage = e.Content as StartPage; if (startPage != null) { if (string.IsNullOrEmpty(startPage.Title)) startPage.Title = "Hello!"; } } public void Uninitialize(InitializationEngine context) { } }
You're not missing anything Martin. There's a bug in admin UI which doesn't display default values that are defined from the code.
Has it been a bug for long? I remember wanting to do something similar to Martin a year and a half ago and back then it did not work with SetDefaultValues either.
Hello Martin
I think I misunderstood your original requirement. You are adding the attribute to an existing instance of a block and you are expecting the default value to be set? If thats the case then thats now how it works. Jafet has explained this and suggested a work around.
It's also summarised in the comments that originally proposed the [Default] attribute based way of setting values:
"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."
https://world.episerver.com/blogs/Per-Magne-Skuseth/Dates/2014/3/Attribute-based-default-values/
David
I misunderstood it too. :)
But for claritys sake, for future visitors, the bug is that if you have defined a default value for a content type property like so:
It will work with setting that value when you create such content types, but it won't be displayed here:
Hello everyone
In a Block a [DefaultValue] is not working. It is an existing Block, existing properties [DefaultValue] is working fine. I added a new property and it won't set the default value. Am I missing something?
This is the new property within the Block