Create content programmatically using strong types from ModelsBuilder
Hello All,
I'm trying to read content from a feed and then save it as Umbraco content.
I have LiveAppModel and a strongly typed classes fro all my document types.
When looking into examples on how to save something I get to examples like this. Where everything is done with properties in string. Meaning if a property name will change, get deleted or change datatype then code will break someday in Live environment.
So there has to be a better way to accomplish this, so if in this case I have an object Person deriving from PublishedContentModel (automatically generated with the Modelsbuilder).
How do I then set it's properties, since when accessing person.Firstname I have the issue that this property is readonly.
Create content programmatically using strong types from ModelsBuilder
Hello All,
I'm trying to read content from a feed and then save it as Umbraco content.
I have LiveAppModel and a strongly typed classes fro all my document types.
When looking into examples on how to save something I get to examples like this. Where everything is done with properties in string. Meaning if a property name will change, get deleted or change datatype then code will break someday in Live environment.
https://our.umbraco.com/documentation/reference/management/services/contentservice/Create-content-programmatically
So there has to be a better way to accomplish this, so if in this case I have an object Person deriving from PublishedContentModel (automatically generated with the Modelsbuilder).
How do I then set it's properties, since when accessing person.Firstname I have the issue that this property is readonly.
Regards, Patrick
Hi Patrick
You can change content only via content service, so you cannot use ModelsBuilder to change the content.
Published content and ModelsBuilder are only for rendering.
Content Service for all database changes.
Thanks,
Alex
Hi Patrick
When using ModelsBuilder you can use a strongly typed syntax if you prefer:
So in case you have this in a class library and the alias had changed, it should give you a compile error.
/Bjarne
@Bjarna, you rock, thanks ! I'll bet many others will find this answer usefull.
You're welcome :)
Great you find it useful. You can find more documentation here regarding extending ModelsBuilder: https://our.umbraco.com/Documentation/Reference/Templating/Modelsbuilder/Understand-And-Extend
is working on a reply...