Context: We are using Sitecore 8.2 and using archive functionality to archive the content. When content is archived then it is only removed from master database so we had a requirement to remove the item from web database also.

Our solution: To achieve the above requirement, we have used custom archival given here. We did modification to set NeverPublish for the item before item is archived and published the item to web so that it is removed from web. However after it is removed from the web, we want to set NeverPublish back to false. If we do it as part of archival function after publishitem function then item is not getting removed from web database.

Question: Based on analysis, it seems that by the time, item is getting published, the NeverPublish is set to false so item is not removed from the web. In order to fix this issue, we need a callback function on publish item method so that we can set NeverPublish false in that callback function after publish is completed. Any inputs are appreciated.

Sample Code

public override Guid ArchiveItem(Item item)
        bool originalNeverPublish = item.Publishing.NeverPublish;

        item.Publishing.NeverPublish = true;

        PublishManager.PublishItem(item, target,item.Languages, false, false, false);

        item.Publishing.NeverPublish = originalNeverPublish;

        return base.ArchiveItem(item);

1 Answer 1


When you use PublishManager.PublishItem method, it returns an object of Sitecore.Handle type.

You can use again PublishManager to wait until the publish is finished. Just use PublishManager.WaitFor function like that:

Sitecore.Handle handle = PublishManager.PublishItem(item, target, item.Languages, false, false, false);
PublishManager.WaitFor(handle, timeout, pollInterval);

It will wait until the publish is finished or for timeout milliseconds (polling every pollInterval milliseconds).


You can get the published item in publish:end event, e.g.:

public void PostProcessPublishedItem(object sender, EventArgs args)
    Publisher publisher = (args as SitecoreEventArgs).Parameters.FirstOrDefault() as Publisher;
    if (publisher.Options.Mode == PublishMode.SingleItem)
        var item = publisher.Options.RootItem;

If you want to make sure that you do your post process only when you are in the process of archiving item, maybe instead of using item.Publishing.NeverPublish, you can use item[FieldIDs.UnpublishDate] and set it to some very specific value, e.g.

item[FieldIDs.UnpublishDate] = "20000102T030405Z";

And them only clear that value in the publish:end event it the value is equal to what you set in your custom archiving method.

  • No doubt this could be the option, only problem with this approach is archival will have to wait until publish is finished which will be blocking user UI, that's why looking for option similar to callback, on publish.
    – Hiren
    Commented Aug 9, 2018 at 20:36
  • So you want to use publish:end event? Like described here briancaos.wordpress.com/2018/01/04/… ?
    – Marek Musielak
    Commented Aug 9, 2018 at 20:44
  • Yes, for the item that I am publishing.
    – Hiren
    Commented Aug 9, 2018 at 20:45
  • Answer updated.
    – Marek Musielak
    Commented Aug 9, 2018 at 20:58
  • I have accepted your answer as there is no callback function for publish that I am looking for so closest I can get is what you have suggested. Thanks.
    – Hiren
    Commented Aug 16, 2018 at 15:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.