I had quite a common requirement with a current Umbraco project I'm working on. Each page can have an infinite number of banner ads on the right hand column of the screen. Easiest way is to create a new document type for each banner item and allow them to be added as sub pages of the main text page. In this particular web site each text page can have its own sub text pages so you end up with a nice tree structure of homepage, child pages and grand-child pages.
If each of these pages can have banner items the visual representation of the tree in the Umbraco manager pages becomes very difficult to understand.
What I needed was a container folder in the tree to place all the banner ads so they don't get mixed up with the text pages.
When expanded you can see the banner items

This necessitated creating the banner 'container' sub-folder automatically each time a new text page was created.
The simplest way to achieve this is to use Umbraco Action Handlers
All the code is shown below but the main part of the Action Handler which is simply a Class Library dumped in the bin folder of the Umbraco site.
We are only interested in trapping the event fired when a page is create(d) otherwise leave the function.
// Only work with create event
if (action.Alias != "create") return true;
Similarly we are only interested in the Text Page document type
// Only work with Text Page docuemnt types
if (documentObject.ContentType.Alias != "Text Page") return true;
Create a new document of type banner container and base the location of the current documentID.
Document bannerItem = null;
bannerItem = Document.MakeNew("Banners", DocumentType.GetByAlias("Banner Container"), documentObject.User, documentObject.Id);
I didn't want this container folder showing up in the navigation or any sitemap so I had to set some properties and then save the object.
bannerItem.getProperty("umbracoNaviHide").Value = "1";
bannerItem.getProperty("siteMapHide").Value = "1";
bannerItem.Save();
That's it.....simple but very useful for creating a specific structure of sub-page when creating a new content node.
Full Code:
using System;
using System.Collections.Generic;
using System.Text;
using umbraco.BusinessLogic.Actions;
using umbraco.BusinessLogic.console;
using umbraco.cms.businesslogic.web;
namespace UmbracoWebsite
{
public class BannerHandler : IActionHandler
{
#region IActionHandler Members
public bool Execute(Document documentObject, umbraco.interfaces.IAction action)
{
// Only work with create event
if (action.Alias != "create") return true;
// Only work with Text Page docuemnt types
if (documentObject.ContentType.Alias != "Text Page") return true;
Document bannerItem = null;
bannerItem = Document.MakeNew("Banners", DocumentType.GetByAlias("Banner Container"), documentObject.User, documentObject.Id);
bannerItem.getProperty("umbracoNaviHide").Value = "1";
bannerItem.getProperty("siteMapHide").Value = "1";
bannerItem.Save();
return true;
}
public string HandlerName()
{
return "BannerHandler";
}
public umbraco.interfaces.IAction[] ReturnActions()
{
return new umbraco.interfaces.IAction[] { new umbraco.BusinessLogic.Actions.ActionNew() };
}
#endregion
}
}