Copied to clipboard

Flag this post as spam?

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


  • Marcelo 18 posts 131 karma points
    Nov 14, 2018 @ 23:37
    Marcelo
    0

    During 'ContentService.UnPublishing' the boolean property 'Published' is already FALSE.

    Hello all,

    I am hooking some events and I noticed something odd, I wrote this code:

    protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
            {
                base.ApplicationStarted(umbracoApplication, applicationContext);
                ContentService.UnPublishing += (sender, e) => ContentService_UnPublishing(sender, e, umbracoHelper);
            }
    
            private void ContentService_UnPublishing(global::Umbraco.Core.Publishing.IPublishingStrategy sender, global::Umbraco.Core.Events.PublishEventArgs<IContent> e, UmbracoHelper umbracoHelper)
            {
                foreach (var node in e.PublishedEntities)
                {
                    if (node.Published)
                    {
                        var publishedNode = umbracoHelper.TypedContent(node.Id);
                    }
    
                }
            }
    

    Basically, I need to get the TypedContent, during the UnPublish event, but the "umbracoHelper.TypedContent()" get only published content, so I had to hook to "ContentService.UnPublishing".

    The question is, why the boolean 'node.Published' is false during the 'ContentService.UnPublishing' event? I believe it should be TRUE, it should be false only on 'ContentService.UnPublished' event, isn't it?

  • Julien Kulker 75 posts 427 karma points c-trib
    Nov 15, 2018 @ 11:08
    Julien Kulker
    0

    Hi,

    I am trying to recreate your situation but i can not reproduce your problem.

    For example if i have a "Home" DocumentType and i do a publish of the Home Page and after that i unpublish it.

    On the unpublish event node.Published is true. So that seems correct.

    protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
            var umbracoHelper = new Umbraco.Web.UmbracoHelper(Umbraco.Web.UmbracoContext.Current);
    
            base.ApplicationStarted(umbracoApplication, applicationContext);
            ContentService.UnPublishing += (sender, e) => ContentService_UnPublishing(sender, e, umbracoHelper);
        }
    
        private void ContentService_UnPublishing(global::Umbraco.Core.Publishing.IPublishingStrategy sender, global::Umbraco.Core.Events.PublishEventArgs<IContent> e, UmbracoHelper umbracoHelper)
        {
            foreach (var node in e.PublishedEntities)
            {
                if (node.Published)
                {
                    var publishedNode = umbracoHelper.TypedContent(node.Id);
                }
    
            }
        }
    

    I use the latest version Umbraco 7.12.4 So maybe update will fix the problem you have.

  • Marcelo 18 posts 131 karma points
    Nov 15, 2018 @ 12:50
    Marcelo
    0

    Hello Julien,

    Thank you for trying to recreate my issue.

    I forgot to mention that this situation only happens when I use the 'Scheduled Publishing' feature, if I click the button 'Unpublish' inside the Content it works ok, but if I schedule an unpublish the boolean always is FALSE during the UnPublishing event.

    My Umbraco is 7.12.3, but sure I am going to update it soon.

  • Julien Kulker 75 posts 427 karma points c-trib
    Nov 15, 2018 @ 13:02
    Julien Kulker
    0

    Hi Marcelo,

    What i did do now setting the publish start date and end date still node.published is true

    Are you sure you published the document atleast once? That is the only thing i can think of

    So make sure you have a published item atleast once.

  • Marcelo 18 posts 131 karma points
    Nov 15, 2018 @ 14:25
    Marcelo
    0

    Hi Julien,

    Yes, the content is 'published' so I schedule an unpublish and during the 'unpublishing' event the boolean is FALSE, I will try to explain with print screens, I believe it will be easier.

    Basically, I am trying to clean my sitemap cache, so I need to call the DistributedCache.Instance.Refresh when some Content is Published and when it is Unpublished for the sitemap keep in sync with the reality. If you have another better idea to this I would be glad to know, because I am new in Umbraco :-)

    1 - This is my Content Info with status 'Published': enter image description here

    2 - So the user decided to unpublish the Content today at 12:17: enter image description here

    3 - At 12:17 the code hitted my Visual Studio during the Unpublishing event, but the boolean is FALSE, which I think it should be TRUE: enter image description here

    Checking the related changes in version 7.12.4 there is only one change. Do you think it would worth for me to update my version from 7.12.3 to 7.12.4?

  • Julien Kulker 75 posts 427 karma points c-trib
    Nov 15, 2018 @ 14:35
    Julien Kulker
    100

    Hi Marcelo,

    What you do is probally "Save and schedule" Doing that will create a new version a version that is not published. so that is why your publish is false. Cause it isn't published.

    If you only have a end date use "Save and publish" Then you have a published version and as soon you hit the unpublish date time then published is true.

    If you have a publish start and unpublish then you can schedule the item.

    So: If you set only unpublish date use "Save and publish" and not Save and schedule

  • Marcelo 18 posts 131 karma points
    Nov 15, 2018 @ 15:15
    Marcelo
    1

    That is it Julien! Finally it makes sense :)

    I am sure if I ask to the users to have attention for this two options they will not then I will change a little bit my code to guarantee the cache will be cleanned in any option choosed during the scheduler task.

    I found this property called 'HasPublishedVersion' which now makes sense after your explanation. My ended code is:

    private void ContentService_UnPublishing(global::Umbraco.Core.Publishing.IPublishingStrategy sender, global::Umbraco.Core.Events.PublishEventArgs<IContent> e, UmbracoHelper umbracoHelper)
            {
                foreach (var node in e.PublishedEntities)
                {
                    if (node.Published || node.HasPublishedVersion)
                    {
                        var publishedNode = umbracoHelper.TypedContent(node.Id);
    
                        if (publishedNode != null && publishedNode.IsVisible())
                        {
                            DistributedCache.Instance.Refresh(Guid.Parse(MWCacheRefresherId.SitemapXmlCacheRefresherId), publishedNode.Site().Id);
                        }
                    }
    
                }
            }
    

    Thank you Julien!

  • Julien Kulker 75 posts 427 karma points c-trib
    Nov 19, 2018 @ 10:10
    Julien Kulker
    0

    woud be great if you could mark it as answered

Please Sign in or register to post replies

Write your reply to:

Draft