Copied to clipboard

Flag this post as spam?

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


  • Markus Johansson 1936 posts 5864 karma points MVP 2x c-trib
    Apr 04, 2022 @ 14:50
    Markus Johansson
    0

    Update content of "Block" inside Block List on save

    I'm trying to update the content of a Rich Text Editor inside a blocklist using a "ContentSavingNotification"-handler `INotificationHandler

    I've managed to get my hands on the content but the changes that I make are not persisted.

    I looks something like this:

    foreach (var entity in notification.SavedEntities) {

    foreach (var property in entity.Properties)
    {
        if (property.PropertyType.PropertyEditorAlias == "Umbraco.BlockList")
        {
    
            string rawJson = property.GetValue().ToString();
    
            BlockValue blockValue = JsonConvert.DeserializeObject<BlockValue>(rawJson);
    
            foreach (var column in blockValue.ContentData)
            {
                foreach (var columnProperty in column.RawPropertyValues)
                {
                    // this contains other blocks
                    var block = JsonConvert.DeserializeObject<BlockValue>(columnProperty.Value.ToString());
    
                    if (block == null)
                        continue;
    
                    foreach (var blockData in block.ContentData)
                    {
                        foreach (var blockProperty in blockData.RawPropertyValues)
                        {
                            if (blockProperty.Key == "body")
                            {
                                blockData.RawPropertyValues["body"] =
                                    new HtmlInputCleaner().CleanUp(blockProperty.Value.ToString());
                            }
                        }
                    }
    
                }
            }
        }
    }
    

    }

    Anyone that has managed to do this? I guess the process is simular for Nested Content and the Block List Editor?

  • Markus Johansson 1936 posts 5864 karma points MVP 2x c-trib
    Apr 04, 2022 @ 15:04
    Markus Johansson
    100

    Ohh... I forgot =D Any changes that I make to the object graph after DeSerializing to Json needs to be serialized back again =D

    Here is a working example:

    public class RichTextEditorCleanupOnSaveHandler : 
    INotificationHandler<ContentSavingNotification>
    {
    public void Handle(ContentSavingNotification notification)
    {
    
        foreach (var entity in notification.SavedEntities)
        {
    
            foreach (var property in entity.Properties)
            {
                if (property.PropertyType.PropertyEditorAlias == "Umbraco.BlockList")
                {
    
                    string rawJson = property.GetValue().ToString();
    
                    BlockValue blockValue = JsonConvert.DeserializeObject<BlockValue>(rawJson);
    
                    foreach (var column in blockValue.ContentData)
                    {
                        foreach (var columnProperty in column.RawPropertyValues)
                        {
                            // this contains other blocks
                            var block = JsonConvert.DeserializeObject<BlockValue>(columnProperty.Value.ToString());
    
                            if(block == null)
                                continue;
    
                            foreach (var blockData in block.ContentData)
                            {
                                foreach (var blockProperty in blockData.RawPropertyValues)
                                {
                                    if (blockProperty.Key == "body")
                                    {
    
                                        blockData.RawPropertyValues["body"] = new HtmlInputCleaner().CleanUp(blockProperty.Value.ToString());
                                    }
                                }
                            }
    
                            column.RawPropertyValues[columnProperty.Key] = JsonConvert.SerializeObject(block);
    
                        }
                    }
    
                    property.SetValue(JsonConvert.SerializeObject(blockValue));
    
                }
            }
    
        }
    }
    }
    
  • Baz 3 posts 73 karma points
    Jul 25, 2024 @ 18:15
    Baz
    0

    I tried this implementation in v14.1 and got an error during the deserialization of the blocklist.

    BlockValue blockValue = JsonConvert.DeserializeObject<BlockValue>(rawJson);
    

    enter image description here

    Is this a bug?

  • Biagio Paruolo 1618 posts 1910 karma points c-trib
    Jul 26, 2024 @ 05:53
    Biagio Paruolo
    0

    Try this:

     var pu = _publishedContentQuery.Content(5686);
            IEnumerable<BlockListItem> lista = new List<BlockListItem>();
            lista = (IEnumerable<BlockListItem>)pu.GetProperty("righePreventivo").GetValue();
            var listaaa = lista.Select(x => x.Content);
    
Please Sign in or register to post replies

Write your reply to:

Draft