using System; using System.Collections.Generic; using System.Linq; using System.Text; using umbraco.BusinessLogic.Actions; using umbraco.BusinessLogic.console; using umbraco.cms.businesslogic.web; namespace Demo { class DateFolderActionHandler : IActionHandler { #region IActionHandler Members public bool Execute(Document documentObject, umbraco.interfaces.IAction action) { // Not interested in anything but "create" events. if (action.Alias != "create") return true; // Not interested if the item being added is not a blog post. if (documentObject.ContentType.Alias != "BlogPost") return true; string year = DateTime.Now.ToString("yyyy"); string month = DateTime.Now.ToString("MM"); Document yearDocument = null; foreach (IconI child in documentObject.Parent.Children) { if (child.Text == year) { yearDocument = new Document(child.Id); break; } } // If the year folder doesn't exist, create it. if (yearDocument == null) { yearDocument = Document.MakeNew(year, DocumentType.GetByAlias("YearFolder"), documentObject.User, documentObject.Parent.Id); } Document monthDocument = null; foreach (IconI child in yearDocument.Children) { if (child.Text == month) { monthDocument = new Document(child.Id); break; } } // If the month folder doesn't exist, create it. if (monthDocument == null) { monthDocument = Document.MakeNew(month, DocumentType.GetByAlias("MonthFolder"), documentObject.User, yearDocument.Id); } // Move the document into the month folder. documentObject.Move(monthDocument.Id); return true; } public string HandlerName() { return "DateFolderActionHandler"; } public umbraco.interfaces.IAction[] ReturnActions() { return new umbraco.interfaces.IAction[] { new umbraco.BusinessLogic.Actions.ActionNew() }; } #endregion } }