Copied to clipboard

Flag this post as spam?

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


  • Braydie 148 posts 346 karma points
    Apr 05, 2012 @ 12:11
    Braydie
    0

    Programmatically remove an item from Examine Index on unpublish

    I have a bit of a weird issue on a 4.7 site whereby a page is unpublished it occassionally remains in the examine index and appears in search results. This can be fixed by reindexing the site but as the site is quite large it isn't really a viable solution to manually reindex every time a problem is reported with search.. 

    I was thinking of perhaps having an event handler fire after unpublish to remove the index entry but I'm not sure if this is the best way to get around this problem. 

    Has anyone come across this issue / got a fix for it?

    Thank!

  • Braydie 148 posts 346 karma points
    Apr 05, 2012 @ 14:41
    Braydie
    2

    I've gone for an event handler to fix this issue.

    class RemoveIndexOnUnpublishEventHandler : ApplicationBase
        {
            public RemoveIndexOnUnpublishEventHandler()
            {
                Document.BeforeUnPublish += new Document.UnPublishEventHandler(Document_BeforeUnPublish);
            }
     
            void Document_BeforeUnPublish(Document sender, umbraco.cms.businesslogic.UnPublishEventArgs e)
            {
                var Nodes = sender.DescendantsAndSelf();
     
                foreach (BaseIndexProvider indexer in ExamineManager.Instance.IndexProviderCollection)
                {
                    foreach (var Page in Nodes)
                    {
                        indexer.DeleteFromIndex(Page.Id.ToString());
                    }
                }
            }
        }

    This event handler gets all the children of the page that has been unpublished and removes their entries from the search indexes.

Please Sign in or register to post replies

Write your reply to:

Draft