Preventing content from being deleted by child nodes in umbraco
I would like to prevent content nodes from being destroyed if they have children. I am setting up an event handler like this:
public class KeepSafeEvents : ApplicationBase
{
public KeepSafeEvents()
{
Document.BeforeMoveToTrash += new Document.MoveToTrashEventHandler(Document_BeforeMoveToTrash);
}
void Document_BeforeMoveToTrash(Document sender, umbraco.cms.businesslogic.MoveToTrashEventArgs e)
{
if (sender.HasChildren)
{
e.Cancel = true;
}
}
}
However, this doesn't work. I guess this is because the deletion process moves the child nodes to the trash first, before accessing the parent node (which then has no children). Is there another possible solution? Or am I making the simple mistake above?
+2
a source to share
1 answer
This code works great for me. Are you sure you copied the resulting DLL file to the Umbraco / bin folder?
I just wrote it a little shorter than you, as shown below, but the functionality should be exactly the same.
I notice that the document with childnode seems to be deleted (it disappears from the tree), but when you reload the tree, the node still exists.
public class KeepSafeEvents : ApplicationBase
{
public KeepSafeEvents()
{
Document.BeforeMoveToTrash += Document_BeforeMoveToTrash;
}
void Document_BeforeMoveToTrash(Document sender, MoveToTrashEventArgs e)
{
if (sender.HasChildren)
e.Cancel = true;
}
}
+1
a source to share