SetDefaultValues for IList of blocks creates default blocks

Vote:
 

Version: 12.25.1

Block is a simple link block with a heading, text and PageReference for target.

We have content that has an IList property of this link block and we would like the property to be created with a couple of links setup by default. We use SetDefaultValues to create a list with a few link blocks setup (using some example values here):

public override void SetDefaultValues(ContentType contentType)
{
    base.SetDefaultValues(contentType);
    AccordionItems = new List<SimpleLinkBlock>
    {
        new SimpleLinkBlock
        {
            ButtonText = "Simple Link Button 1",
            Heading = "Heading Test 1",
            Target = ContentReference.StartPage
        },
        new SimpleLinkBlock
        {
            ButtonText = "Simple Link Button 2",
            Heading = "Heading Test 2",
        }
    };
}

The result when creating a new instance of the Content is a list of 2 SimpleLinkBlock's that are completely empty. The values we set does not get copied. 

Is there a workaround that does not involve moving SimpleLinkBlock away from being a block and creating descriptors?

#312941
Nov 22, 2023 11:41
Vote:
 

Hi Glenn Adrian,

You should create instance like this instead:

var blockPropertyFactory = ServiceLocator.Current.GetInstance<IBlockPropertyFactory>();
var block1 = blockPropertyFactory.Create<SimpleLinkBlock>();
block1.ButtonText = "Simple Link Button 1";
block1.Heading = "Heading Test 1";
block1.Target = ContentReference.StartPage;

var block2 = blockPropertyFactory.Create<SimpleLinkBlock>();
block2.ButtonText = "Simple Link Button 2";
block2.Heading = "Heading Test 2";

TestBlocks = new List<SimpleLinkBlock> { block1, block2 };

Reference: List Properties (optimizely.com)

#313705
Dec 05, 2023 12:51
Glenn Adrian - Dec 06, 2023 16:32
Thank you, I did find an alternative method of setting the property in the Property dictionary directly which worked but this is cleaner.

This however only works for the first layer of Lists. I have another situation where I have a list of a block which contains another list. I can set the default values of the first list but the next layer ignores anything I try to do.
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.