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() { }
}
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() { }
}
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.
You're triggering a Publish within the Unpublish event, so yeah, that will trigger the Publishing event again ;-)
Maybe just save the document instead?
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 {
Thanks for answering!
is working on a reply...