Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Laurence Gillian 600 posts 1219 karma points
    Oct 11, 2020 @ 14:35
    Laurence Gillian
    0

    Umbraco.BlockList > Getting value in C# (without ModelsBuilder)

    Hello,

    I am playing with the new Block List editor.

    Rather than using Models Builder, I would like to grab the value using the old fashioned GetValue<T> approach from the IContent.

    If I look at the stored value, I can see some JSON coming back, but I was under the assumption that somewhere in the pipeline a value converter would kick in, and I'd be able to cast to something useful?

    Perhaps I've used Models Builders for too long, and I'm missing something?

    public class Page
        {
            public Page(IContent content)
            {
                this.Title = content.GetValue<string>("title");
    
                // (behind the scenes this is a big blob of JSON)
                // when cast, returns null.          
                this.BodyBlocks = content.GetValue<BlockListModel>("bodyBlocks");
            }
    
            public string Title { get; }
            public IEnumerable<BlockListItem> BodyBlocks { get; }
        }
    
  • Laurence Gillian 600 posts 1219 karma points
    Oct 11, 2020 @ 16:03
    Laurence Gillian
    2

    Figured out, the property value converters are not run on IContent only IPublishedContent. However, as I am working within the content publishing pipeline, I don't have IPublishedContent yet.

    Can't see an immediate solution to that, so I am using JsonConvert to grab the data I require.

    public class Page
    {
        public Page(IContent content)
        {
            this.Title = content.GetValue<string>("title");
    
            var json = content.GetValue("bodyBlocks").ToString();
            var bodyBlocks = JsonConvert.DeserializeObject<BlockEditorPropertyValue>(json);
    
            this.BodyBlocks = bodyBlocks.ContentData;
        }
    
        public string Title { get; }
        public List<object> BodyBlocks { get; }
    }
    
    public class BlockEditorPropertyValue
    {
        public object Layout { get; set; }
        public List<object> ContentData { get; set; }
        public object SettingsData { get; set; }
    }
    
  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies