Recycle bin won’t empty when using EPiServer Relate

A few days ago i found i strange behaviour on our intranet site. The waste basket didn’t empty! The scheduled job ran just fine, but the log always contained the same message: 0 content items were deleted from recycle bin

Thats a new one! So where to look? The logs are, off course, a great place to start looking for errors. The first line that cought my attention was this one: ERROR EPiServer.Util.EmptyWastebasketJob: 17.3.1 Error when trying to delete content from recycle bin

At first, i thought it was some kind of permission issue. The site runs Windows authentication, and that has some side effects, but than i found another line in the log: EPiServer.Core.TypeMismatchException: Content with id ‘<xxxxx>’ is of type ‘Castle.Proxies.FormContainerBlockProxy’ which does not inherit required type ‘EPiServer.Core.PageData’

and than another similar line: EPiServer.Core.TypeMismatchException: Content with id ‘29990’ is of type ‘EPiServer.Core.ContentFolder’ which does not inherit required type ‘EPiServer.Core.PageData’

I couldn’t believe there’s still code in EPiServer that requires PageData!! And, off couse, there wasn’t. BUT! EPiServer Relate is another story. The CmsIntegrationModule had a few fingers in that cookie jar!

To be specific, the method RemovePageEntity, which deletes corresponding page entities when a page is deleted:

        /// <summary>
        /// Deletes the correspoinding <see cref="CMSPageEntity"/> when a page is deleted
        /// </summary>
        /// <param name="e">The <see cref="EPiServer.PageEventArgs"/> instance containing the event data.</param>
        private static void RemovePageEntity(PageEventArgs e)
        {
            if (e == null || PageReference.IsNullOrEmpty(e.PageLink))
            {
                return;
            }
            
            CMSPageEntity pagedataEntity = CMSPageEntityHandler.Instance.GetEntity(e.Page.PageGuid, e.Page.LanguageBranch, 0);

            if (pagedataEntity == null)
            {
                return;
            }

            CMSPageEntityHandler.Instance.RemoveEntity(pagedataEntity);
        }

It first, it looks like the code really do try to avoid null references by checking if PageEventArgs are null and than if PageEventArgs.PageLink is null, but: when you have a ContentFolder or FormContiner in the recycle bin, both of them are set. The problem is that PageEventArgs.Page is null.

So, just throw in one more null check for e.Page, like this:

        /// <summary>
        /// Deletes the correspoinding <see cref="CMSPageEntity"/> when a page is deleted
        /// </summary>
        /// <param name="e">The <see cref="EPiServer.PageEventArgs"/> instance containing the event data.</param>
        private static void RemovePageEntity(PageEventArgs e)
        {
            if (e == null || e.Page == null || PageReference.IsNullOrEmpty(e.PageLink))
            {
                return;
            }
            
            CMSPageEntity pagedataEntity = CMSPageEntityHandler.Instance.GetEntity(e.Page.PageGuid, e.Page.LanguageBranch, 0);

            if (pagedataEntity == null)
            {
                return;
            }

            CMSPageEntityHandler.Instance.RemoveEntity(pagedataEntity);
        }

And you also need to handle errors in GetClubId. I took the easy way and put a try around the code.

        private static int GetClubId(PageReference pageReference)
        {
            try
            {
                if (PageReference.IsNullOrEmpty(pageReference))
                {
                    return -1;
                }

                // Get the page
                PageData page = DataFactory.Instance.GetPage(pageReference);

                // Check if it is an article page, if not return
                if (page is ArticlePage && ((ArticlePage)page).ClubAssociation > 0)
                {
                    return ((ArticlePage)page).ClubAssociation;
                }
                return -1;
            }
            catch { }

            return -1;
        }

Now the recycle bin should empty just fine!

Leave a Reply

Your email address will not be published. Required fields are marked *