Copied to clipboard

Flag this post as spam?

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


  • Filip Axbrink Jönsson 3 posts 73 karma points
    Jun 05, 2019 @ 06:48
    Filip Axbrink Jönsson
    0

    Unpublishing content triggers Published event

    Hello,

    When content is published for the first time I use the ContentService.Published event to set the property publishDate. But I also want to null the publishDate on ContentService.Unpublishing / Unpublished. With the code below the Published event is triggered when I unpublish content in Umbraco 8, instead of the Unpublishing / Unpublished (tried both). ContentService_Unpublished is never entered.

    public class EventsComponentComposer : ComponentComposer<EventsComponent> { }
    
    public class EventsComponent : IComponent
    {
    
        public EventsComponent() { }
    
        public void Initialize()
        {
            ContentService.Published += ContentService_Published;
            ContentService.Unpublishing += ContentService_Unpublished;
        }
    
        private void ContentService_Unpublished(IContentService sender, PublishEventArgs<IContent> e)
        {
            foreach (IContent node in e.PublishedEntities.Where(m => m.HasProperty("publishDate")))
            {
                var content = sender.GetById(node.Id);
                content.SetValue("publishDate", null);
                sender.SaveAndPublish(content);
            }
        }
    
        private void ContentService_Published(IContentService sender, ContentPublishedEventArgs e)
        {
            foreach (IContent node in e.PublishedEntities.Where(m => m.HasProperty("publishDate")))
            {
                var existingValue = node.GetValue("publishDate");
                if (existingValue == null)
                {
                    var content = sender.GetById(node.Id);
                    content.SetValue("publishDate", DateTime.Now);
                    sender.SaveAndPublish(content);
                }
            }
        }
    
        public void Terminate() { }
    }
    
  • Sebastiaan Janssen 5045 posts 15476 karma points MVP admin hq
    Jun 05, 2019 @ 08:42
    Sebastiaan Janssen
    1

    You're triggering a Publish within the Unpublish event, so yeah, that will trigger the Publishing event again ;-)

    Maybe just save the document instead?

  • Filip Axbrink Jönsson 3 posts 73 karma points
    Jun 05, 2019 @ 08:53
    Filip Axbrink Jönsson
    0

    Haha this is a good remark, I'll have to change that when I get the method to work. Sadly it doesn't fix my current problem, that it never actually enters ContentService_Unpublished.

    This code also enters ContentService_Published when i unpublish content:

    public class EventsComponent : IComponent {

        public EventsComponent() { }
    
        public void Initialize()
        {
            ContentService.Published += ContentService_Published;
        }
    
        private void ContentService_Published(IContentService sender, ContentPublishedEventArgs e)
        {
            foreach (IContent node in e.PublishedEntities.Where(m => m.HasProperty("publishDate")))
            {
                var existingValue = node.GetValue("publishDate");
                if (existingValue == null)
                {
                    var content = sender.GetById(node.Id);
                    content.SetValue("publishDate", DateTime.Now);
                    sender.SaveAndPublish(content);
                }
            }
        }
    
        public void Terminate() { }
    }
    

    Thanks for answering!

Please Sign in or register to post replies

Write your reply to:

Draft