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)
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.
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
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);
}
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.
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.
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
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.
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
Then add something like this :-
And then put the code in a function like this:-
Hi Elin,
You'll need to create the class yourself.
In Visual Studio create a new class called: ApplicationEvents
Give it a using of Umbraco.Core
Create another function like this:-
Then finally create your function and the code from my original post.
Hope this helps
Thanks
Paul
Thanks for helping with this Paul.
I'll give that a try and hopefully I get it working.
Thanks again.
Elin
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.
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
is working on a reply...