Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Andrew Gibson 16 posts 107 karma points
    Dec 01, 2017 @ 11:13
    Andrew Gibson
    0

    publishing event, delete the content

    I have a MainSidebar Document Type and I've written code in the publishing event to prevent more than one of these being created, code below.

    This works, and the publishing is prevented, but a faded content document still exists in the content area that needs to be deleted. How can I also delete, or discard, the content please?

    [There is ent.ChangeTrashedState possibly(?).]

        public class Subscription : ApplicationEventHandler
    {
        protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
            base.ApplicationStarted(umbracoApplication, applicationContext);
    
            Umbraco.Core.Services.ContentService.Publishing += ContentService_Publishing;
        }
    
        private void ContentService_Publishing(Umbraco.Core.Publishing.IPublishingStrategy sender, Umbraco.Core.Events.PublishEventArgs<Umbraco.Core.Models.IContent> e)
        {
            foreach (var ent in e.PublishedEntities)
            {
                if (ent.ContentType.Alias.ToUpper() == "MAINSIDEBAR")
                {
                    e.CancelOperation(new Umbraco.Core.Events.EventMessage("Sidebar", "Only one sidebar allowed", Umbraco.Core.Events.EventMessageType.Error));
    
                    LogHelper.Error(typeof(Subscription), $"Attempt to create additional sidebar(s) (writer id: {ent.WriterId})", new ApplicationException());
                }
            }
        }
    }
    
  • Andrew Gibson 16 posts 107 karma points
    Dec 01, 2017 @ 11:58
    Andrew Gibson
    0

    Darn, it just occurred to me that I should use the Creating event, doi! That should hopefully solve.

  • Andrew Gibson 16 posts 107 karma points
    Dec 01, 2017 @ 12:09
    Andrew Gibson
    0

    I now have a similar issue with the Creating version, in that it still goes into the document properties, and it no longer shows my notification.

    Looking for some hints ;)

        public class Subscription : ApplicationEventHandler
    {
        protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
            base.ApplicationStarted(umbracoApplication, applicationContext);
    
            Umbraco.Core.Services.ContentService.Creating += ContentService_Creating;
        }
    
        private void ContentService_Creating(IContentService sender, NewEventArgs<IContent> e)
        {
            if (e.Alias.ToUpper() == "MAINSIDEBAR")
            {
                e.CancelOperation(new Umbraco.Core.Events.EventMessage("Sidebar", "Only one sidebar allowed", Umbraco.Core.Events.EventMessageType.Error));
    
                LogHelper.Error(typeof(Subscription), $"Attempt to create additional sidebar", new ApplicationException());
            }
        }
    }
    
  • Dan Diplo 1554 posts 6205 karma points MVP 5x c-trib
    Dec 01, 2017 @ 12:37
    Dan Diplo
    0

    The way I've done it in the past has been something like:

    private void ContentService_Creating(IContentService sender, NewEventArgs<IContent> e)
    {
        if (e.Alias.ToUpper() == "MAINSIDEBAR" && e.CanCancel)
        {
            e.Cancel = true;
            e.Messages.Add(new Umbraco.Core.Events.EventMessage("Sidebar", "Only one sidebar allowed", Umbraco.Core.Events.EventMessageType.Error));
        }
    }
    

    Not sure if that works better?

    If you want to log, I wouldn't raise an exception - use a warn instead.

  • Andrew Gibson 16 posts 107 karma points
    Dec 01, 2017 @ 13:09
    Andrew Gibson
    0

    Thank you.

    This runs, and it executes e.Cancel = true; but nothing happens in the UI/backoffice: the content is saved and appears in the tree.

    I also tried using Created, as a notice in VS says that Creating is obsolete, but, again, nothing happens.

  • Andrew Gibson 16 posts 107 karma points
    Dec 01, 2017 @ 13:20
    Andrew Gibson
    0

    I tried using Saving, and it kind-of worked, but generated an additional (404) error and left the backoffice in an indeterminate state (just with a loading icon).

    I've reverted to Publishing (with a Warn log) for the moment, but it's not as good as I'd like it :(

            private void ContentService_Publishing(Umbraco.Core.Publishing.IPublishingStrategy sender, Umbraco.Core.Events.PublishEventArgs<Umbraco.Core.Models.IContent> e)
        {
            foreach (var ent in e.PublishedEntities)
            {
                if (ent.ContentType.Alias.ToUpper() == "MAINSIDEBAR")
                {
                    e.CancelOperation(new Umbraco.Core.Events.EventMessage("Sidebar", "Only one sidebar allowed", Umbraco.Core.Events.EventMessageType.Error));
    
                    LogHelper.Warn(typeof(Subscription), $"Attempt to create additional sidebar(s) (writer id: {ent.WriterId})");
                }
            }
        }
    
Please Sign in or register to post replies

Write your reply to:

Draft