How to get block list from icontent?
# help-with-umbraco
a
We are needing to do a one-time bulk update of 700+ content pages. I know how to manipulate the block list and create new blocks. The issue I'm having is that I can't seem to get the current blocks from IContent. The property alias is "mainContent" but
p.GetValue("mainContent")
returns null. I found [this forum post](https://our.umbraco.com/forum/using-umbraco-and-getting-started/112682-work-with-blocklist-properties-using-the-umbraco-content-service-v12) but the
content.GetValue("content").ToString()
line is where I'm having trouble. Any insight?
c
Hey @ahwm Had to do similar recently where we were updating a color palette choice which was nested in the SettingsData of a block list. Paraphrasing what we did in code but it was like this:
Copy code
csharp

var query = serviceScope.ServiceProvider.GetRequiredService<IPublishedContentQuery>();
var allNodes = query.ContentAtRoot().SelectMany(x => x.DescendantsOrSelf());

foreach (var node in allNodes)
{
     var sectionPicker = node.Value<IEnumerable<BlockListItem>>("sectionPicker");
     var contentNode = _contentService.GetById(node.Id);
     var jsonValue = contentNode.GetValue("sectionPicker").ToString();
     BlockValue blockValue = JsonConvert.DeserializeObject<BlockValue>(jsonValue);

     foreach (var column in blockValue.SettingsData)
     {
          foreach (var columnProperty in column.RawPropertyValues.ToList())
          {
                    // Detect and edit the particular property value here.
          }
     }
}


//In our case, sectionPicker was the block list
Happy to send the full file for what we were updating (we were migrating a color picker property to a contentment data list. But might have some useful stuff for you).
a
That certainly looks helpful. I did notice I could get it from IPublishedContent, but then I realized the site had some unpublished (scheduled) updates and so I switched to the content service to get everything so I could update those as well. It's only a small handful, so maybe we'll just have to update those manually. What is
serviceScope
? Your excerpt didn't include its type/definition 🙂
c
Copy code
csharp

using var _ = _umbracoContextFactory.EnsureUmbracoContext();
            using var serviceScope = _serviceProvider.CreateScope();
My apologies 😄
And service provider is DIed in private readonly IServiceProvider _serviceProvider;
a
Thanks. I actually was able to figure that out, but I still ended up with
GetValue<BlockValue>()
returning null. After digging through the object a bit, I came up with this:
Copy code
var main = p!.Properties.First(x => x.Alias == "mainContent").Values.First().PublishedValue!.ToString()!;
For whatever reason, that works.
81 Views