#Custom Block List Property

1 messages · Page 1 of 1 (latest)

cinder rock
#

I have a custom blocklist property as part of a document type. It's very simple and only accepts one block component type. I am trying to access this property in my code. While following the umbraco documentation here: https://docs.umbraco.com/umbraco-cms/fundamentals/backoffice/property-editors/built-in-umbraco-property-editors/block-editor/block-list-editor,

It tells me it returns an IEnumerable<BlockListItem>. On that same page, it says that I can pull the value as that or BlockListModel... but with either, it returns null.

if (content.HasProperty("locations"))
{
    var locations = content.GetValue<BlockListModel>("locations");
    (or)
    var locations = content.GetValue<IEnumerable<BlockListItem>>("locations");
    ...
}

If I pull the value as a string or dynamic object, it will show as:

{"layout":{"Umbraco.BlockList":[{"contentUdi":"umb://element/74437d1928534a9287430056e140b99b"},{"contentUdi":"umb://element/382ddba6783948b28696798b3b6dad16"}]},"contentData":[{"contentTypeKey":"0285ad7f-763f-4bc5-80be-45f85f8a39e6","udi":"umb://element/74437d1928534a9287430056e140b99b","city":"Colville","stateA":"[\"WA\"]","name":"Colville Toyota"},{"contentTypeKey":"0285ad7f-763f-4bc5-80be-45f85f8a39e6","udi":"umb://element/382ddba6783948b28696798b3b6dad16","stateA":"[\"OR\"]","name":"Coos Bay Toyota","city":"Coos Bay"}],"settingsData":[]}

In converting to the BlockListModel, it has to be erroring causing it to return null; right? I need to pull the values this way because I am using those values to calculate other properties and then save that property to the document.

Anyone running into this or have a solution? Is this enough info? I feel like I've tried quite a bit without success.

sick grail
#

Blocklist json data being converted to the BlockListModel happens in a propertyvalueconverter, so if you are accessing this data at a point where it hasn't yet run through the converter it will not work as it will just be a string of json data.

So the big question is, in what type of code are you accessing it, and how?

zinc wind
#

The only other thing is you have a property which has a list

var locations = content.GetValue<IEnumerable<BlockListItem>>("locations");

you need to go through the list and process it as it does't look strongly typed.. ( i may be missing something )

I'd also ask what @sick grail asked< in what type of code are you accessing it ( I take it you are doing this in a razor page..)

cinder rock
#

@sick grail @zinc wind Thanks for your input. I am accessing it in a controller method, specifically a ContentSavingNotification. (var content in notification.SavedEntities.Where...)

sick grail
#

In that case your notifications.SavedEntities are of the type IContent IIRC, which will not yet be converted.

Which means that you need to work with it as json, potentially serializing it to a c# model yourself

cinder rock
#

Ah, I see. In this case, am I able to manipulate the accessed data and save it back to the content/object through something like content.setValue("locations", json) ?

sick grail