tobbbe
6/27/2016 - 12:43 PM

Prevent nodes from being deleted

public class MyApplication : ApplicationEventHandler
    {
        protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
            ContentService.Trashing += PreventDelete;
        }

        void PreventDelete(IContentService sender, MoveEventArgs<IContent> e)
        {
            // if any of deleted nodes has an ancestor with doctype "App"
            if (e.MoveInfoCollection.Any(n => n.Entity.Ancestors().Any(x => x.ContentType.Alias == "App")))
            {
                e.CancelOperation(new EventMessage("App", "Can't be deleted", EventMessageType.Error));
            }
        }
    }