Press Ctrl / CMD + C to copy this to your clipboard.
This post will be reported to the moderators as potential spam to be looked at
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.
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.
When using ModelsBuilder you can use a strongly typed syntax if you prefer:
using ContentModels = Umbraco.Web.PublishedModels;
var contentService = Services.ContentService;
var person = contentService.Create("John Doe", parentId, "person");
person.SetValue(ContentModels.Person.GetModelPropertyType(x => x.FirstName).Alias, "John");
person.SetValue(ContentModels.Person.GetModelPropertyType(x => x.LastName).Alias, "Doe");
So in case you have this in a class library and the alias had changed, it should give you a compile error.
@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:
is working on a reply...
Write your reply to:
Image will be uploaded when post is submitted