Copied to clipboard

Flag this post as spam?

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


  • Keith 12 posts 91 karma points
    Mar 28, 2024 @ 19:07
    Keith
    0

    Create a page programmatically

    Hi

    I'm looking for some guidance on how to create a node using C#.

    I have a document type created and want to create a few pages dynamically using code and the created document type as the page type in Umbraco 13.

    Does anyone have any ideas?

  • Marc Goodson 2157 posts 14411 karma points MVP 9x c-trib
    Mar 28, 2024 @ 22:10
    Marc Goodson
    1

    Hi Keith

    Have a look at the ContentService

    https://docs.umbraco.com/umbraco-cms/reference/management/services/contentservice

    You can inject the Content Service into the Controller/Class where you want to create your content.

    You then use the CreateContent method to establish the name of the node you are creating, provide the id of the node under which you want to create it, plus the alias of the type of content you are creating...

    ... then you can use the SetValue method to set each property's value...

    Finally you call SaveAndPublish to persist the new content item to the database and the published cache...

    Regards

    Marc

  • Keith 12 posts 91 karma points
    Mar 29, 2024 @ 06:27
    Keith
    0

    Hi Marc

    Thanks I will take a look.

    "provide the id of the node under which you want to create"

    What if I don't know the GUID/ID of the node as it doesn't exist? Could I pass in a fake one or do I need to do this differently?

    Finally as for the SetValue, could I use strongly typed nodes if I use ModelsBuilder to generate models?

    Thanks

  • Marc Goodson 2157 posts 14411 karma points MVP 9x c-trib
    Mar 29, 2024 @ 10:55
    Marc Goodson
    100

    Hi Keith

    Usually the context within you are creating the thing, will enable you to get access to the parent, eg, you might have just created the parent :-P or be creating something after a page has published etc....

    If you provide -1 for the parentId, it will create the content item at a the root of the content tree.

    So if you are importing lots of things from a spreadsheet, you might first query the content tree to see if there is a content item in the root of the site that already exists,under which you will create your imported items - and if it doesn't create it using -1 as the parent, then when you save and publish that folder or when you find it, you'll have access to it's Id to use when running your import....

    The SetValue is used for each property and uses it's alias as a string...

    Modelsbuilder is generally used for rendering rather than being involved in the ContentService.... but you could use your generated modelsbuilder classes to give you some intellisense into the property names... this used to be fairly straightforward called to GetModelPropertyType eg

    MyGeneratedModel.GetModelPropertyType(x=>x.PageTitle).Alias
    

    but I'm vaguely remembering there is something more to it, there is this blog post here that might explain if the above doesn't 'just' work anymore: https://umbracare.net/blog/how-to-get-umbraco-model-property-alias/

    regards

    marc

Please Sign in or register to post replies

Write your reply to:

Draft