Custom Block List Property
j
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. On that same page, it says that I can pull the value as that or BlockListModel... but with either, it returns null.
Copy code
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:
Copy code
{
  "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.
j
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?
r
The only other thing is you have a property which has a list var locations = content.GetValue<IEnumerable>("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 @Jemayn asked< in what type of code are you accessing it ( I take it you are doing this in a razor page..)
j
@Jemayn @Ravi Thanks for your input. I am accessing it in a controller method, specifically a
ContentSavingNotification
. (
var content in notification.SavedEntities.Where...
)
j
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
j
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)
?
j
Technically yes, I'd be very careful though as the blocklist model structure has to be exactly right 🙂 This is the closest to a helpful resource that I know about: https://docs.umbraco.com/umbraco-cms/fundamentals/backoffice/property-editors/built-in-umbraco-property-editors/block-editor/block-list-editor#creating-blocklist-programmatically
j
Thank you!!
253 Views