I'm trying to cancel the unpublish event of a content item, but it doesn't seem to work. I've created a class which uses the applicationbase class and adds a BeforeUnpublish event to the document. In this event, I set the eventsargs.cancel to true, make some changes and republish the document. But the item 'll still be unpublished. Here a sample of my code.
public class CancelUnpublish :ApplicationBase {
public CancelUnpublish() { Document.BeforeUnPublish += new Document.UnPublishEventHandler(Document_BeforeUnpublish); }
protected void Document_BeforeUnpublish(Document sender, umbraco.cms.businesslogic.UnPublishEventArgs e) { if (sender.ContentType.Alias == "alias of documenttype") { // cancel the unpublish event e.Cancel = true; // remove connections from document sender.getProperty("propertyname").Value = string.Empty; // republish the document sender.Publish(umbraco.helper.GetCurrentUmbracoUser()); } } }
I looks like you are probably getting the first line cancelled with the event, but the node is still removed from the cache.
So even though you try to cancel it, and event if you try to update the cache yourself, the library.UnPublishSingleNode(_document.Id); will run afterwards and remove it from the cache.
Looks like a bug to me, so I would suggest putting it on codeplex if it is not there already.
This is a side effect of the devided architecture in Umbraco; you cancel the IO unpublish (which prepares the document for unpublishing in the db), but that doesn't know about the runtime unpublish (which is in the umbraco.content class), so you'll need to cancel umbraco.content.BeforeClearDocumentCache (or something like that - this is on top of my head).
Should make a little sense if you know how to publish via the API where you also need to do
1) Document.Publish() <= Prepares the Document object for publishing and versioning in the DB 2) umbraco.library.UpdateDocumentCache <= Tells the runtime to refresh the cache
Cancel unpublish event
Hello,
I'm trying to cancel the unpublish event of a content item, but it doesn't seem to work. I've created a class which uses the applicationbase class and adds a BeforeUnpublish event to the document. In this event, I set the eventsargs.cancel to true, make some changes and republish the document. But the item 'll still be unpublished. Here a sample of my code.
public class CancelUnpublish :ApplicationBase
{
public CancelUnpublish()
{
Document.BeforeUnPublish += new Document.UnPublishEventHandler(Document_BeforeUnpublish);
}
protected void Document_BeforeUnpublish(Document sender, umbraco.cms.businesslogic.UnPublishEventArgs e)
{
if (sender.ContentType.Alias == "alias of documenttype")
{
// cancel the unpublish event
e.Cancel = true;
// remove connections from document
sender.getProperty("propertyname").Value = string.Empty;
// republish the document
sender.Publish(umbraco.helper.GetCurrentUmbracoUser());
}
}
}
What am I doing wrong?
This is the code that runs when unpublishing a node in the backend UI
I looks like you are probably getting the first line cancelled with the event, but the node is still removed from the cache.
So even though you try to cancel it, and event if you try to update the cache yourself, the library.UnPublishSingleNode(_document.Id); will run afterwards and remove it from the cache.
Looks like a bug to me, so I would suggest putting it on codeplex if it is not there already.
This is a side effect of the devided architecture in Umbraco; you cancel the IO unpublish (which prepares the document for unpublishing in the db), but that doesn't know about the runtime unpublish (which is in the umbraco.content class), so you'll need to cancel umbraco.content.BeforeClearDocumentCache (or something like that - this is on top of my head).
Should make a little sense if you know how to publish via the API where you also need to do
1) Document.Publish() <= Prepares the Document object for publishing and versioning in the DB
2) umbraco.library.UpdateDocumentCache <= Tells the runtime to refresh the cache
Hope this helps!
Niels...
When I cancel the content.beforeClearDocumentCache and the unpublish event, the item isn't unpublished.
Thanks Niels!
is working on a reply...