tags:

views:

82

answers:

1

I would like to prevent content nodes from being trashed if they have any children. I setup an event handler like so:

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 seem to work. I assume it is because the delete process moves the child nodes to trash first before dealing with the parent node (which then has no children). Is there another possible solution? Or am I making a simple mistake above?

+1  A: 

This code works perfectly for me. Are you sure that you've copied the resulting .dll file to Umbraco's /bin folder?

I just wrote it a little shorter than you did, like below, but the functionality should be exactly the same.

I do notice that the document with childnode seems to get deleted (it disappears from the tree), but when you reload the tree, the node is still there.

public class KeepSafeEvents : ApplicationBase
{
  public KeepSafeEvents()
  {
    Document.BeforeMoveToTrash += Document_BeforeMoveToTrash;
  }

  void Document_BeforeMoveToTrash(Document sender, MoveToTrashEventArgs e)
  {
    if (sender.HasChildren)
      e.Cancel = true;
  }
}
sebastiaan
Yeah, you're right the code works at preventing the delete; the animation just doesn't halt. I entered a bug into the umbraco project.
Soldarnal