Copied to clipboard

Flag this post as spam?

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


  • Martin Overgaard 9 posts 41 karma points
    Oct 27, 2014 @ 14:35
    Martin Overgaard
    0

    Programmatically create childnode when creating parent node in Umbraco 7.1.x

    Hi there,

    I'm trying to figure out how to create an child node programmatically when I create a parent node. I know that I can use ContentService to create the child node like:

     

    using Umbraco.Core;
    using Umbraco.Core.Models;
    using Umbraco.Core.Services;

    // get the Umbraco Content Service var contentService = Services.ContentService; var childnode = contentService.CreateContent( name, 1000, "myDocType", 0); // if i would set some values product.SetValue("price", myPrice); // then save and publish it contentService.SaveAndPublish(product);

    The thing I don't know is how to activate this code when my parent node is saved/published in umbraco.

    Can someone point me in the right direction?

    /Martin

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Oct 27, 2014 @ 15:01
    Jan Skovgaard
    100

    Hi Martin

    I think you can benefit from the list of events found in the documentation here http://our.umbraco.org/documentation/Reference/Events-v6/ContentService-Events - Should be possible to hook into the save or saving events I think.

    Don't worry to much about the reference points to something with "v6" in it. It's for v6+ - A little confusing, I know! :)

    Hope this helps.

    /Jan

  • Martin Overgaard 9 posts 41 karma points
    Oct 27, 2014 @ 20:54
    Martin Overgaard
    0

    Hi Jan,

    That seems to be the right direction, I will test it tomorrow and give some feedback on it thanks ;o)

    /Martin

  • Martin Overgaard 9 posts 41 karma points
    Oct 28, 2014 @ 08:44
    Martin Overgaard
    2

    Hi Jan,

    I had the time to test it and it works. Thanks for your time.

    Just for reference here is what I did. In a class library I created the class below and added a reference to the library in my Umbraco website.

    using Umbraco.Core;
    using Umbraco.Core.Events;
    using Umbraco.Core.Models;
    using Umbraco.Core.Publishing;
    using Umbraco.Core.Services;

    namespace Your.Namespace
    {
        public class CreateSubDocument : ApplicationEventHandler
        {
            public CreateSubDocument()
            {
                ContentService.Published += Go;
            }

            private void Go(IPublishingStrategy sender, PublishEventArgs args)
            {
                foreach (var node in args.PublishedEntities)
                {
                    if (node.ContentType.Alias == "YourDocType")
                    {
                        // get the Umbraco Content Service
                        var contentService = new ContentService();
                        var childnode = contentService.CreateContent(
                            "Product",
                            node.Id,
                            "YourSubDocType",
                            0);

                        // if i would set some values
                        childnode.SetValue("MetaTitle", "Product");

                        // then save and publish it
                        contentService.SaveAndPublishWithStatus(childnode);
                    }
                }
            }
        }
    }
  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Oct 28, 2014 @ 08:59
    Jan Skovgaard
    0

    Hi Martin

    Happy to held and thank you for sharing so others can benefit, should they come across this post.

    High 5 You Rock! :)

    /Jan

  • Victor 25 posts 146 karma points
    May 16, 2018 @ 20:28
    Victor
    0

    As of 7.9.2, the snippet above doesn't work. I had to get the ContentService from a static instance of ApplicationContext instead of creating a new class from ContentService and change the cast type of PublishEventArgs. I managed to do this with the following code:

    using Umbraco.Core;
    using Umbraco.Core.Events;
    using Umbraco.Core.Models;
    using Umbraco.Core.Publishing;
    using Umbraco.Core.Services;
    
    namespace UmbracoMobil.Utils
    {
        public class CreateSubDocument : ApplicationEventHandler
        {
            protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
            {
                ContentService.Published += Go;
            }
    
            private void Go(IPublishingStrategy sender, PublishEventArgs<IContent> args)
            {
                foreach (var node in args.PublishedEntities)
                {
                    if (node.ContentType.Alias == "YourNode")
                    {
                        // get the Umbraco Content Service
                        var contentService = ApplicationContext.Current.Services.ContentService;
    
                        var childnode = contentService.CreateContent("Child name", node.Id, "childDocAlias", 0);
    
                        // if i would set some values
                        //childnode.SetValue("MetaTitle", "Product");
    
                        // then save and publish it
                        contentService.SaveAndPublishWithStatus(childnode);
                    }
                }
            }
        }
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft