Copied to clipboard

Flag this post as spam?

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


  • Paul de Quant 403 posts 1520 karma points
    Dec 18, 2014 @ 09:32
    Paul de Quant
    0

    Creating Child Nodes on Page Save

    Hello,

    I'm currently using Umbraco 7.2 and I'm trying to create an application event on the Saving event to automatically create 4 child nodes on the page I've just saved.

    While I believe I've got the Event handler right, I don't know what code I need to create these child pages.

    So:-

    > Home

    >> My Section

    >>> Parent 1 (So I create this page as a child page of 'My Section')

    >>>> Sub 1 (These pages should be created automatically)

    >>>> Sub 2 (These pages should be created automatically)

    >>>> Sub 3 (These pages should be created automatically)

    >>>> Sub 4 (These pages should be created automatically)

    Any help will be greatly appreciated.

    Thanks

    Paul

  • Paul de Quant 403 posts 1520 karma points
    Dec 18, 2014 @ 10:48
    Paul de Quant
    102

    Answered my own question:-

                var savedEnts = e.SavedEntities;

                int parentID = savedEnts.Select(x => x.Id).FirstOrDefault();

                string folderName = savedEnts.Select(x => x.Name).FirstOrDefault();

                IContentType templateType = savedEnts.Select(x => x.ContentType).FirstOrDefault();

                bool IsNew = savedEnts.Select(x => x.IsNewEntity()).FirstOrDefault();

     

                if (templateType.Alias == "Profile" && IsNew == true)

                {

                    ContentService c = new ContentService();

                    var content = c.CreateContent("Sub 1", parentID, "Content");

                    content.SetValue("pageTitle", "Sub 1");

                    c.Save(content);

     

                    content = c.CreateContent("Sub 2", parentID, "Content");

                    content.SetValue("pageTitle", "Sub 2");

                    c.Save(content);

     

                    // Create Media Folder

                    IMediaService mediaService = ApplicationContext.Current.Services.MediaService;

                    var newFolder = mediaService.CreateMedia(folderName, 1110, "Folder");

                    mediaService.Save(newFolder);

     

                }

  • Ido Shahar 3 posts 73 karma points
    Aug 12, 2015 @ 07:12
    Ido Shahar
    0

    Hi Paul, Can you please explain where did you put this code step by step? I really need to do this action until tomorrow. Please.

    Thanks in advance. Ido.

  • Elin 45 posts 166 karma points
    Jan 23, 2018 @ 13:33
    Elin
    0

    Hi Ido,

    Have you ever figured out where exactly to add Paul's code ?

    I need to do something similar but I'm not sure where exactly to start with.

    Thanks.

  • Paul de Quant 403 posts 1520 karma points
    Jan 23, 2018 @ 13:38
    Paul de Quant
    0

    Hi Elin,

    I created a new class 'ApplicationEvents' which inherited IApplicationEventHandler

    In this class you can you can create a function called: ContentService_Publishing and add the code there.

    Cheers Paul

  • Elin 45 posts 166 karma points
    Jan 23, 2018 @ 13:46
    Elin
    0

    Thanks for replying Paul,

    Where is the ApplicationEventHandler located exactly? I think that's what I'm having the most trouble with, as I don't know where to find/create this file.

    Thanks.

  • Paul de Quant 403 posts 1520 karma points
    Jan 23, 2018 @ 13:49
    Paul de Quant
    0

    You need to create it yourself. If you're using Visual studio just create a new class call ApplicationEventHandler and add the:-

    using Umbraco.Core;

    to the head

     public class ApplicationEvents : IApplicationEventHandler { }
    

    Then add something like this :-

    public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext) { ContentService.Publishing += this.ContentService_Publishing; }
    

    And then put the code in a function like this:-

    public void ContentService_Publishing(Umbraco.Core.Publishing.IPublishingStrategy sender, Umbraco.Core.Events.PublishEventArgs<IContent> e) {}
    
  • Paul de Quant 403 posts 1520 karma points
    Jan 23, 2018 @ 14:00
    Paul de Quant
    1

    Hi Elin,

    You'll need to create the class yourself.

    In Visual Studio create a new class called: ApplicationEvents

    public class ApplicationEvents : IApplicationEventHandler {}
    

    Give it a using of Umbraco.Core

    Create another function like this:-

    public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext) { ContentService.Publishing += this.ContentService_Publishing; }
    

    Then finally create your function and the code from my original post.

    public void ContentService_Publishing(Umbraco.Core.Publishing.IPublishingStrategy sender, Umbraco.Core.Events.PublishEventArgs<IContent> e) { }
    

    Hope this helps

    Thanks

    Paul

  • Elin 45 posts 166 karma points
    Jan 23, 2018 @ 14:04
    Elin
    0

    Thanks for helping with this Paul.

    I'll give that a try and hopefully I get it working.

    Thanks again.

    Elin

  • Mark Bowser 273 posts 860 karma points c-trib
    Aug 12, 2015 @ 17:15
    Mark Bowser
    0

    Ido, at least in Umbraco 6.1.0+ you need to do something like the following. It is done a little differently in older versions.

    public class MyUmbracoEvents : ApplicationEventHandler
    {
        protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
            ContentService.Saved += CreateChildNodes;
        }
    
        private static void CreateChildNodes(IContentService cs, SaveEventArgs<IContent> e)
        {
            // all that stuff he did goes here.
        }
    }
    

    Make a class that inherits from ApplicationEventHandler and on ApplicationStarted, register some saved event handlers.

    Here is some documentation about registering these sorts of events. It talks about how to do it in various umbraco versions: application-startup - events & event registration

Please Sign in or register to post replies

Write your reply to:

Draft