ContentSavingNotification
# help-with-umbraco
u
Hi! Is there any method through which I can map properties inside SavedEntities in a similar way to IPublishedContent? I would like to validate properties based on the BlockList (single item) and check if they have valid values.
Copy code
public void Handle(ContentSavingNotification notification)
    {
        IContent? entity = notification.SavedEntities.SingleOrDefault();

        if (entity is null)
        {
            return;
        }

        foreach (var prop in entity.Properties)
        {
            // map properties from block grid to a model
            // custom validation
        }
    }
p
You should be able to do
entity.GetValue<Type>("alias")
just like you do on the IPublishedContent items
d
In my experience this does not actually work the same as published content. You can only insert a particular set of primitive types as type parameter or else you either get return value
null
or an exception. Which exact types, I'm not sure
You can get the blocklist model as
Umbraco.Cms.Core.Models.Blocks.BlockValue
with the method that Patrick shared. I'm not sure if you can input that type as type parameter or if you have to grab it as string and then convert it with a json serializer, but that way, you can get the raw model that is saved to the database.
j
The problem is IContent has the raw data which is saved in the database. So for block things it'll be a JSON blob. When it gets converted into IPublishedContent it runs through the valueconverter which converts things like UDIs to other IPublishedContent models, etc. So if you want to get a similar strongly typed block model in your notification you'd have to reimplement all the logic from the valueconverter.
d
Or... Just use the existing valueconverter by injecting
PropertyValueConverterCollection
u
currently I am using expando object and dynamic but I am not happy with this approach as I am losing type checks 😭
9 Views