Copied to clipboard

Flag this post as spam?

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


  • Maarten Peters 5 posts 25 karma points
    Aug 27, 2010 @ 15:09
    Maarten Peters
    0

    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?

     

     

     

     

     

  • Morten Bock 1867 posts 2140 karma points MVP 2x admin c-trib
    Aug 27, 2010 @ 15:21
    Morten Bock
    0

    This is the code that runs when unpublishing a node in the backend UI

    protected void UnPublishDo(object sender, System.EventArgs e)
    {
     _document.UnPublish();
     littPublishStatus.Text = ui.Text("content", "itemNotPublished", base.getUser());
     UnPublish.Visible = false;
    
     library.UnPublishSingleNode(_document.Id);
      //newPublishStatus.Text = "0";
    }   

    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.

  • Niels Hartvig 1951 posts 2391 karma points c-trib
    Aug 27, 2010 @ 15:42
    Niels Hartvig
    1

    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...

     

     

  • Maarten Peters 5 posts 25 karma points
    Aug 30, 2010 @ 11:06
    Maarten Peters
    0

    When I cancel the content.beforeClearDocumentCache and the unpublish event, the item isn't unpublished.

    Thanks Niels!

     

Please Sign in or register to post replies

Write your reply to:

Draft