Copied to clipboard

Flag this post as spam?

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


  • Richard Jackson 17 posts 127 karma points c-trib
    Sep 06, 2023 @ 08:21
    Richard Jackson
    0

    Create content programmatically, or "Where do you just run code?"

    In the Create content programmatically section there is a C# code snippet which is just run the once to create content, and is then never used again.

    Question: Where should this code snippet actually be run? In what file? Where should this be so that Umbraco / ASP.NET Core runs this? As a composer? As a [what]?

    While this is a question with that specific page in mind, there are myriad code snippets like this where I, a beginner, have no idea where they should actually be run.

  • Marc Goodson 2142 posts 14345 karma points MVP 8x c-trib
    Sep 06, 2023 @ 09:09
    Marc Goodson
    100

    Hi Richard

    Yes, that page, appears entirely out of Context, Why would you do this?, Where would you do this? Show me the code sample in context in a real world thing!

    I think as the page lives in the reference section it's meant to be a quick lookup/reminder of the code rather than an introduction to it, but that only makes sense if it's introduced elsewhere.

    The community docs team did start a repo that aimed to accompany the documentation and try to show the samples from the docs in the context of the site, that actually would run, and have all the right namespaces at the top.

    It lives here: https://github.com/umbraco/Umbraco.Docs.Samples

    And is open to contribution, of course it doesn't have any specific examples of using the ContentService to CreateContent (yet) - but it has some examples of using the ContentService to update Content which is similar, one here for programmatically setting Media Crops on a Content Item:

    https://github.com/umbraco/Umbraco.Docs.Samples/blob/578ef82b15a24c1c0c0c9cd29cf8ab3f86423c7e/Umbraco.Docs.Samples.Web/Property-Editors-Add-Values/CreateImageCropperValuesController.cs#L15

    The key question with this specific page in mind is what are you trying to do that has led you to look at creating content programmatically, as this will determine where that code would be used.

    eg I can think of a few common scenarios.

    A one-off import from another system into Umbraco Content Items or In response to an event, eg Creating a set of content items whenever another content item is created, eg if a News Article is saved, you might want to programmatically create a year and month content items if they don't exist and move the article to live under the appropriate Month or In response to a Form post on the front end of the site, eg to store a comment or details of a form etc

    I wonder if it would be useful to add one of those scenarios to the Umbraco.Docs.Samples project to 'show this off' in context. What would be most useful?

    Anyway more specifically, with regard to Where, it depends on what you are trying todo...

    So if you are running an import, you'll likely have a controller endpoint that can be called directly via Url to trigger the import or perhaps responds to a form file upload, and into this Controller c# class, you can inject the ContentService to do your creating... (https://docs.umbraco.com/umbraco-cms/reference/routing/umbraco-api-controllers)

    If your creation should respond to something in Umbraco being published then you would create a c# class that implements INotificationHandler, there are some good samples in the repo for showing this off:

    https://github.com/umbraco/Umbraco.Docs.Samples/blob/578ef82b15a24c1c0c0c9cd29cf8ab3f86423c7e/Umbraco.Docs.Samples.Web/Notifications/CulturePublishingCheck.cs

    You can use Composers/Components to register your custom notification handlers, however with .Net Core the recommended approach is to extend IUmbracoBuilder, eg see this sample:

    https://github.com/umbraco/Umbraco.Docs.Samples/blob/578ef82b15a24c1c0c0c9cd29cf8ab3f86423c7e/Umbraco.Docs.Samples.Web/Notifications/UmbracoBuilderNotificationExtensions.cs

    And you'll see in startup.cs a call to this extension method AddDocsSamplesNotifications()

    You can inject the ContentService into this notification handler to do your content creation.

    Not sure if this all helps!

    regards

    Marc

  • Richard Jackson 17 posts 127 karma points c-trib
    Sep 06, 2023 @ 09:24
    Richard Jackson
    0

    What a brilliant response. Thank you so much, I'll explore this in depth later. #H5YR!

  • Richard Jackson 17 posts 127 karma points c-trib
    Sep 06, 2023 @ 09:42
    Richard Jackson
    0

    "What are you trying to do that has led you to look at creating content programmatically"

    I'm currently working on a website migration from a static-site generated page (Hugo/Netlify CMS) with 100s of markdown files as blog articles, which I am wanting to convert into Umbraco pages built around using the Block List Editor.

    There are details on programmatically creating content with the Block List Editor here but I found some difficulty with understanding them (for example, there's a JSON file and two C# files, but I can't see how the JSON file is referenced by either of the C# files), and so went back to a basic understanding of the Content Service, which led me to this question!

Please Sign in or register to post replies

Write your reply to:

Draft