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

Creating default values for properties on pages

Vote:
0

Hi.

I already know of SetDefaultValues on PageData, but as far as I know this only executes when the page instance is created.

public override void SetDefaultValues(ContentType contentType)
{...}

Is there a way to set default properties on a page that already exists? And only if the property is NULL?

#228324
Edited, Sep 23, 2020 13:08
Carl S - Sep 24, 2020 4:53
I don't see how this will help since I only need it to run once and at startup.
So if I use IInitializableModule maybe and get the items.
Ravindra S. Rathore - Sep 25, 2020 7:09
Hi Carl S, The content event will trigger when you publish/create/delete the content so you can check your NULL condition there
Carl S - Sep 25, 2020 7:13
Hi Ravindra S. Rathore, Yep I know. But as I wrote before. I need it to be created at startup. Cannot wait for the user to publish the item.
Ravindra S. Rathore - Sep 25, 2020 7:55
Use the CreatingContent or CreatedContent based on your need.
https://marisks.net/2017/01/22/episerver-content-events-explained/
Vote:
0

You can create a scheduled job, or an admin tool that will:

  • Get all pages of a specific type
  • For every page
    • Check if the property is null
      • Create writable clone of the page
      • Update property
      • Save

Or, lets say your property is of type string, and you want to set it's default value some time after adding it.

You could probably do:

[PropertyDefinitionTypePlugIn]
public class PropertyStringWithDefaultHackValue : PropertyString
{
     public override object Value => base.Value ?? "Hack";
}

And then change your property on your content type to:

[Display(Name = "This should work")]
[BackingType(typeof(PropertyStringWithDefaultHackValue))]
public virtual string MyString { get; set; }
#228332
Edited, Sep 23, 2020 15:17
Carl S - Sep 24, 2020 4:55
It is not a string, it's a custom property type, IList<..> and this contains a IList<...>.
I'll try a IInitializableModule and see if I can solve it that way.
Carl S - Sep 24, 2020 5:30
I think this worked even with an IList<...> have to test it further.
Carl S - Sep 24, 2020 5:30
Thanks for now.
Carl S - Sep 25, 2020 6:58
Tomas Hensrud Gulla, your solution does not work for propertystring.. it worked for a IList for some reason.
Vote:
0

Ok tried the IInitializableModule and get an exception:

The property XXX is read-only

How do I make it writable? There is no overload called "MakeWritable" or something as far as I can see.

Note! This is a IList that I try to initiate from null in this instance.

#228362
Edited, Sep 24, 2020 5:16
Tomas Hensrud Gulla - Sep 25, 2020 7:51
CreateWritableClone() og the PageData object.
Vote:
0

What Tomas suggested does not work. At least not in a already custom property type. Any other ideas?

public class CustomInnerProperty
{
     [Display(Order = 900, Name = "Id")]
     [BackingType(typeof(StringAsGuidDescriptor))]
     public virtual string Id { get; set; } = Guid.NewGuid().ToString();

}

[PropertyDefinitionTypePlugIn]
public class StringAsGuidDescriptor : PropertyString
{
    public override object Value
    {
       get => string.IsNullOrEmpty(base.String) ? Guid.NewGuid().ToString() : base.String;
       set => SetPropertyValue(value, () => base.String = value?.ToString());
    }
}

public class CustomInnerPropertyDescriptor : CollectionEditorDescriptor<CustomInnerProperty>
{
   public CustomInnerPropertyDescriptor()
   {
      GridDefinition.DisableDndSorting = true;
   }
}

public class CustomOuterProperty
{
   [Required]
   [Display(Order = 100, Name = "Items")]
   [EditorDescriptor(EditorDescriptorType = typeof(CustomInnerPropertyDescriptor))]
   public virtual IList<CustomInnerProperty> Items { get; set; }
}
#228512
Sep 28, 2020 12:13
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.