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?
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.
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':
2 - So the user decided to unpublish the Content today at 12:17:
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:
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?
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
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);
}
}
}
}
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:
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?
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.
I use the latest version Umbraco 7.12.4 So maybe update will fix the problem you have.
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.
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.
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':
2 - So the user decided to unpublish the Content today at 12:17:
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:
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?
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
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:
Thank you Julien!
woud be great if you could mark it as answered
is working on a reply...