Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Patrick van Kemenade 101 posts 339 karma points
    Apr 09, 2021 @ 12:53
    Patrick van Kemenade
    0

    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

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Apr 09, 2021 @ 14:14
    Alex Skrypnyk
    1

    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

  • Bjarne Fyrstenborg 1284 posts 4038 karma points MVP 8x c-trib
    Apr 09, 2021 @ 15:01
    Bjarne Fyrstenborg
    2

    Hi Patrick

    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("firstName", "John");
    //person.SetValue("lastName", "Doe");
    
    person.SetValue(ContentModels.Person.GetModelPropertyType(x => x.FirstName).Alias, "John");
    person.SetValue(ContentModels.Person.GetModelPropertyType(x => x.LastName).Alias, "Doe");
    
    contentService.Save(person);
    

    So in case you have this in a class library and the alias had changed, it should give you a compile error.

    /Bjarne

  • Patrick van Kemenade 101 posts 339 karma points
    Apr 12, 2021 @ 16:02
    Patrick van Kemenade
    0

    @Bjarna, you rock, thanks ! I'll bet many others will find this answer usefull.

  • Bjarne Fyrstenborg 1284 posts 4038 karma points MVP 8x c-trib
    Apr 12, 2021 @ 18:06
    Bjarne Fyrstenborg
    0

    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

Please Sign in or register to post replies

Write your reply to:

Draft