Document.New eventhandler doesn't seem to work in 4.5
Hi Guys
I'm trying to update my small Runway News Package to work with umbraco 4.5. For some reason the Action Handler that create year/month/day subfolders doesn't seem to work any more. Here's the code:
using System;
using umbraco;
using umbraco.BusinessLogic;
using umbraco.cms.businesslogic.web;
///<summary>
/// UmbracoNewsHandler handles events that are related to the Umbraco News
Document.New eventhandler doesn't seem to work in 4.5
Hi Guys
I'm trying to update my small Runway News Package to work with umbraco 4.5. For some reason the Action Handler that create year/month/day subfolders doesn't seem to work any more. Here's the code:
using System;
using umbraco;
using umbraco.BusinessLogic;
using umbraco.cms.businesslogic.web;
/// <summary>
/// UmbracoNewsHandler handles events that are related to the Umbraco News
/// </summary>
///
namespace umbraco.appstract
{
public class UmbracoNewsItemHandler : ApplicationBase
{
public UmbracoNewsItemHandler()
{
Document.New += new Document.NewEventHandler(Document_New);
//umbraco.cms.businesslogic.web.Document.New += new umbraco.cms.businesslogic.web.Document.NewEventHandler(Document_New);
umbraco.BusinessLogic.Log.Add(umbraco.BusinessLogic.LogTypes.Debug, -1, "The eventhandlers are attached");
}
/// <summary>
/// Event handler for new news pages
/// </summary>
/// <param name="sender">The newly created document</param>
/// <param name="e"></param>
public void Document_New(umbraco.cms.businesslogic.web.Document sender, umbraco.cms.businesslogic.NewEventArgs e)
{
if (sender.ContentType.Alias == "NewsPage")
{
if (sender.Parent != null)
{
umbraco.cms.businesslogic.web.Document parent = new umbraco.cms.businesslogic.web.Document(sender.Parent.Id);
if (sender.ContentType.Alias == "NewsPage" && parent.ContentType.Alias == "NewsLibrary")
{
try
{
//Get the current date
DateTime now = DateTime.Now;
string currentYear = now.Year.ToString();
string currentMonth = now.Month.ToString();
string currentDay = now.Day.ToString();
//Search for current year
umbraco.cms.businesslogic.web.Document currentYearDocument = GetChild(sender.Parent, currentYear, "NewsYear");
//Search for current month
umbraco.cms.businesslogic.web.Document currentMonthDocument = GetChild(currentYearDocument, currentMonth, "NewsMonth");
//Search for current Day
umbraco.cms.businesslogic.web.Document currentDayDocument = GetChild(currentMonthDocument, currentDay, "NewsDay");
sender.Move(currentDayDocument.Id);
sender.Save();
}
catch (Exception)
{
}
}
}
}
}
/// <summary>
/// Checks whether the parent of a document is published, so that the document can also be published.
/// If the parent is not published the method is called on the parent.
/// </summary>
/// <param name="documnet">An unpublished document that wants to be published</param>
/// <param name="topParentAlias">Alias of the top parent alias which stops the recursive parent publish search</param>
/// <returns>Boolean representing whether the parent was published or not</returns>
private bool PublishParent(umbraco.cms.businesslogic.web.Document document, string topParentAlias)
{
if (document.ContentType.Alias != topParentAlias)
{
if (document.Parent != null)
{
umbraco.cms.businesslogic.web.Document parent = new umbraco.cms.businesslogic.web.Document(document.Parent.Id);
if (!parent.Published)
{
if (PublishParent(parent, topParentAlias))
{
document.Publish(umbraco.BusinessLogic.User.GetUser(0));
return true;
}
else
return false;
}
else
{
document.Publish(umbraco.BusinessLogic.User.GetUser(0));
return true;
}
}
else
return false;
}
else
return false;
}
/// <summary>
/// Searches a document to find out weather it contains a child based on name and documentTypeAlias
/// </summary>
/// <param name="parent">The document whose children are to be searched</param>
/// <param name="documentName">The document name to look for</param>
/// <param name="documentTypeAlias">The alias of the document type to look for</param>
/// <returns></returns>
private static umbraco.cms.businesslogic.web.Document GetChild(umbraco.cms.businesslogic.CMSNode parent, string documentName, string documentTypeAlias)
{
umbraco.cms.businesslogic.web.Document document = null;
if (parent.HasChildren)
{
umbraco.cms.businesslogic.web.Document parentDoc = new umbraco.cms.businesslogic.web.Document(parent.Id);
foreach (umbraco.cms.businesslogic.web.Document child in parentDoc.Children)
{
if (child.Text == documentName)
{
if (child.ContentType.Alias == documentTypeAlias)
document = child;
}
}
}
if (document == null)
{
document = umbraco.cms.businesslogic.web.Document.MakeNew(documentName, umbraco.cms.businesslogic.web.DocumentType.GetByAlias(documentTypeAlias), umbraco.BusinessLogic.User.GetUser(0), parent.Id);
document.Save();
}
return document;
}
}
}
Can anybody tell me what has caused this breaking change?
Thanks a lot in advance :)
Kim
You can find the Runway News Package here.
http://our.umbraco.org/projects/runway-news-library
is working on a reply...