Changing BlockList Content programatically, Requir...
# help-with-umbraco
c
Hey All, Another post RE my ongoing migration. (Background: Baseline Project rolling out a property change to multiple children). Attempting to change the value of one property in a block list to another programatically. Going through documentation, it looks like you have to rebuild the entire JSON data of a blocklist when attempting to adjust a property from within? Or am I mistaken and it is actually possible to say: Query for the Block List items via a service and then simply do (similar to the content service) a .SetValue("") call on the individual property I'm attempting to change?
j
Last I looked into it I couldn't find a way to work with it well outside of IPublishedContent. So fx in notifications it was a pain. I eventually ended up dropping it as the json generation also had issues and it was turning into a huge task for a small upside.
d
You do indeed need to edit the json in its entirety. However, you can make your life somewhat easier by converting the json to
BlockValue
first. You get a semi-stronglytyped model that you can edit slightly easier than just the raw json
c
Cheers @D_Inventor ! Didnt know about BlockValue, I had started being suuuper dirty and started converting it into a Dynamic Object! Shall take a look at BlockValue
Ok yeah thats far easier without all the special JSON characters in there. Thank you again!
For anyone that ever needs a small example this is that BlockValue in use, in conjunction with grabbing a Color Picker:
Copy code
csharp
var contentNode = ContentService.GetById(node.Id);

            var jsonValue = contentNode.GetValue("sectionPicker").ToString();

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

            foreach(var settingObj in blockValue.SettingsData)
            {
                foreach (var settingColumnProperty in settingObj.RawPropertyValues)
                {
                    if(settingColumnProperty.Key == "backgroundColor")
                    {
                        var colorObj = settingColumnProperty.Value.ToString();
                        var color = JsonConvert.DeserializeObject<ColorPickerValueConverter.PickedColor>(colorObj);

                        <pre>
                            @color.Label
                            @color.Color
                        </pre>
                        
                    } 
                }
            }
This is a simple "output it into a pre tag on a page" - Ofcourse, dont normally hit the content service on a view for prod 😛
2 Views