November Happy Hour will be moved to Thursday December 5th.

SetDefaultValue for properties of IList<T>

Vote:
 

Hi there,

Does anyone know if it's possible to set default values for a type being used within an IList property?

We've currently got something like the below: user selects a page reference, and a select option. We want to give that select a default value, but can't workout how to do it (if you can). Thought the usual SetDefaultValues would do the trick, but unfortuntely not.

public class IssuesBlock : BaseBlock
{
    public virtual string? Heading { get; set; }
    public virtual IList<IssueCard>? Issues { get; set; }
}

public class IssueCard : BlockData
{
    [SelectOne(SelectionFactoryType = typeof(BackgroundThemeSelectionFactory))]
    public virtual string? BackgroundTheme { get; set; }

    public virtual PageReference? Page { get; set; }
    public override void SetDefaultValues(ContentType contentType)
    {
        base.SetDefaultValues(contentType);
        BackgroundTheme = "blue";
    }
}

I'm aware we can probably just use a ContentArea to get around this, but would be nicer from an editors POV to utilize the IList property. 

So if anyone has any ideas/solutions, let me know :)

Thank you,
Neal

#313865
Dec 07, 2023 17:43
Ted - Dec 12, 2023 14:55
I would expect that to work, so you might have come across a bug.
I'm assuming the default value is properly set if you create an IssueCard block yourself, i.e. not through the IList
Vote:
 

We had a similar problem with IList and blocks. A workaround we found that might work here was adding the properties to the content property collection directly. We ended up not using the workaround so I apologize if I get some details wrong. IList has several bugs currently which makes them hard to work with.

public override void SetDefaultValues(ContentType contentType)
{
    base.SetDefaultValues(contentType);
    if (Property["BackgroundTheme"] is null)
    {
        Property.Add(
            new PropertyString
            { 
                Name = "BackgroundTheme",
                Value = "blue"
            });
    }
    else
    {
        Property["BackgroundTheme"].Value = "blue";
    }
}
#315859
Edited, Jan 19, 2024 12:04
* 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.