Add content programmatically to Umbraco 9

Back in 2020, I created a blog post about how to add content to your Umbraco site programmatically in Umbraco 8.

Things have moved on since then and the code has changed slightly so here is a very that will work in Umbraco 9 and I think it will also work in Umbraco 10, which is due out this week!!

I had the requirement to add a lot of data in to a project so that I can test pagination and filtering. In the past, I would add each content node manually but this time consuming.

Here is how you can do it using the Content Service.

// Include this at the top of your razor view

@inject IContentService ContentService
@using Umbraco.Cms.Core.Services

// Then add this anywhere on your page, I tend to add it just below the @inherits statement.


@{

    var parentNodeIdWithChildToCopy = 18208;
    for(var i = 0; i < 2; i++)
    {
        var children = ContentService.GetPagedChildren(parentNodeIdWithChildToCopy, 0, 904, out var totalRecords);
        foreach(var child in children)
        {
            String[] titleHeading = new string[]
            {
               "rainstorm","preach","weary","gun","plain","zany","helpful","long","various","development","foamy","melted","narrow","freezing","reduce","burly","price","curl","bell",
                "distribution","glow","turkey","meddle","men","boundless","scratch","excuse","mature","post","file","unsuitable","writer","eatable","magical","mere","tray","bump","spotted",
            "volcano","squash","hushed","maddening","smooth","edge","tongue","scorch","gainful","please","decide","porter","jaded","ski","yoke","hospital","mask","barbarous","bubble","business",
            "normal","ashamed","underwear","superb","bore","tedious","beginner","pigs","disagree","earth","verse","perpetual","scattered","rhetorical","workable","cuddly","furry","seemly","puzzled","load",
            "sloppy","ludicrous","queen","ethereal","religion","psychotic","nifty","puzzling","truthful","connection","vulgar","lumpy","bake","bike","mixed","clover","flag","deafening","soak","flaky","statement","lucky"
                     };

            Random rnd = new Random();
            var copiedChild = ContentService.Copy(child, parentNodeIdWithChildToCopy, false);
            copiedChild.Name = "News with " + titleHeading[rnd.Next(titleHeading.Length)];

            if (copiedChild.HasProperty("titleHeadingOne"))
            {
                copiedChild.SetValue("titleHeadingOne", copiedChild.Name);
            }

            ContentService.SaveAndPublish(copiedChild);

        }
    }

}

Before you run the code

In the backoffice, you need to have a parent folder setup with a single child beneath it:

Before running the code

The child is what you will be copying, in this case, the "Learn with javascript" node is what gets copied.

You will also need to make a note of the Partents ID and update the "parentNodeIdWithChildToCopy" variable to have the same ID. In my case the September node had an ID of 18208.

The code explained

This code is dropped in to a razor view that you are able to load from the front end, I put it in a my Home template file but you could drop it in to your Masterpage for example.

When you load up your site for the first time after adding the code, the code will run and duplicate the child node and give it a unique name.

This is when you need to be careful, the 'for' loop can really go crazy. You start with 1 item which then duplicates and now you have 2 times, the next time the loop goes round, you go from 2 to 4 items. I made the mistake of putting the 'for' loop to 10.

2, 4, 8, 16, 32, 64, 128, 256, 512, 1024.

Yip, I ended up with 1025 items under my September folder! I deleted everything and dialed it back!

Folder content after running the code

You can now see I have 4 items with random names from the array of strings and each one has been saved and published.

Now you should delete all the code from your page so that it doesn't run again.

Published on : 14 June 2022