Does anyone have an example of adding a
# package-development
h
Does anyone have an example of adding a block to an existing BlockGrid datatype in code? I have a package that creates some elements to use as blocks and when it installs I want to create the relevant blocks in an exiting block grid(if it exists)
h
j
As I understand it you'd basically have to do something like this: 1. Find a blocklist 2. Get it's content (as json) 3. Deserialize it to a model 4. Insert your own block in both the list of blocks and in the settings list 5. Serialize it to a json string 6. Save the json blocklist to the property via contentservice Every time I've had to programatically work with blocklist I've ended up going another way as it's currently very complicated
h
given up for now, just can't figure it out at all
d
@CodeSharePaul may have some insights from his node creation when importing data
c
@huwred I've done some work with programmatically creating block lists data types and populating their content in Umbraco for QuickBlocks and a site scraping tool I wrote, not done the grid yet. @Jemayn is right with his approach.
h
My problem is I don't know how to get the DataType what service should I use? everything I've tried doesn't return anything that is remotely useful
c
@huwred Here's some code I wrote for populating a block list property
It may or may not be helpful to you
h
Thanks @CodeSharePaul & @Jemayn I have got a bit further in that I can get hold of my blockgrid datatype and I can see that I need to add a block to Configuration.Blocks which I can do, but how do I save this back to my blockgrid dataType?
c
I know it's not block grid
h
I changed your code to be BlockGrid instead of BlockList and I can quite happily create a new blockgrid datatype (one step forward) my issue is that I don't want to create a new blockgrid, I want to update an existing one.
OK, I'm an idiot 🤣 all I had to do was set the Key and Id etc on my new DataType and then save it 😄
Copy code
cs
            var editor = _propertyEditorCollection.First(x => x.Alias == "Umbraco.BlockGrid");
            var grid = _dataTypeService.GetDataType(Guid.Parse("9490ca3c-7a5e-4be3-aa73-712136b2a5a1"));

            var existingblocks = ((BlockGridConfiguration)grid.Configuration).Blocks.ToList();
            //add the new blocks
            existingblocks.AddRange(blocks);

            ((BlockGridConfiguration)grid.Configuration).Blocks = existingblocks.ToArray();

            var newDataType = new DataType(editor, _configurationEditorJsonSerializer)
            {
                Name = grid.Name,
                Configuration = (BlockGridConfiguration)grid.Configuration,
                ParentId = grid.ParentId,
                Key = grid.Key,
                Id = grid.Id,
            };

            _dataTypeService.Save(newDataType);
c
Great news @huwred
d
I like these happy threads 😁
j
This is great! Have an upcoming project where this is likely required so I'm bookmarking this 😄
h
Thanks for the pointers, I now have it working as I wanted. Imminent package release coming
2 Views