{"id":454,"date":"2016-11-04T11:43:33","date_gmt":"2016-11-04T10:43:33","guid":{"rendered":"http:\/\/jonika.nu\/JonasBlogg\/?p=454"},"modified":"2016-11-04T11:43:33","modified_gmt":"2016-11-04T10:43:33","slug":"recycle-bin-wont-empty-when-using-episerver-relate","status":"publish","type":"post","link":"https:\/\/jonika.nu\/JonasBlogg\/archives\/454","title":{"rendered":"Recycle bin won&#8217;t empty when using EPiServer Relate"},"content":{"rendered":"<p>A few days ago i found i strange behaviour on our intranet site. The waste basket didn&#8217;t empty! The scheduled job ran just fine, but the log always contained the same message: <strong>0 content items were deleted from recycle bin<\/strong><\/p>\n<p>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: <strong>ERROR EPiServer.Util.EmptyWastebasketJob: 17.3.1 Error when trying to delete content from recycle bin<\/strong><\/p>\n<p>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: <strong>EPiServer.Core.TypeMismatchException: Content with id &#8216;&lt;xxxxx&gt;&#8217; is of type &#8216;Castle.Proxies.FormContainerBlockProxy&#8217; which does not inherit required type &#8216;EPiServer.Core.PageData&#8217;<\/strong><\/p>\n<p>and than another similar line: <strong>EPiServer.Core.TypeMismatchException: Content with id &#8216;29990&#8217; is of type &#8216;EPiServer.Core.ContentFolder&#8217; which does not inherit required type &#8216;EPiServer.Core.PageData&#8217;<\/strong><\/p>\n<p>I couldn&#8217;t believe there&#8217;s still code in EPiServer that requires PageData!! And, off couse, there wasn&#8217;t. BUT! EPiServer Relate is another story. The <strong>CmsIntegrationModule <\/strong>had a few fingers in that cookie jar!<\/p>\n<p>To be specific, the method RemovePageEntity, which deletes corresponding page entities when a page is deleted:<\/p>\n<pre class=\"lang:c# decode:true \">        \/\/\/ &lt;summary&gt;\r\n        \/\/\/ Deletes the correspoinding &lt;see cref=\"CMSPageEntity\"\/&gt; when a page is deleted\r\n        \/\/\/ &lt;\/summary&gt;\r\n        \/\/\/ &lt;param name=\"e\"&gt;The &lt;see cref=\"EPiServer.PageEventArgs\"\/&gt; instance containing the event data.&lt;\/param&gt;\r\n        private static void RemovePageEntity(PageEventArgs e)\r\n        {\r\n            if (e == null || PageReference.IsNullOrEmpty(e.PageLink))\r\n            {\r\n                return;\r\n            }\r\n            \r\n            CMSPageEntity pagedataEntity = CMSPageEntityHandler.Instance.GetEntity(e.Page.PageGuid, e.Page.LanguageBranch, 0);\r\n\r\n            if (pagedataEntity == null)\r\n            {\r\n                return;\r\n            }\r\n\r\n            CMSPageEntityHandler.Instance.RemoveEntity(pagedataEntity);\r\n        }<\/pre>\n<p>It first, it looks like the code really do try to avoid null references by checking if <strong>PageEventArgs<\/strong> are null and than if <strong>PageEventArgs.PageLink<\/strong> is null, but: when you have a <strong>ContentFolder<\/strong> or <strong>FormContiner<\/strong> in the recycle bin, both of them are set. The problem is that PageEventArgs.Page is null.<\/p>\n<p>So, just throw in one more null check for e.Page, like this:<\/p>\n<pre class=\"lang:c# decode:true\">        \/\/\/ &lt;summary&gt;\r\n        \/\/\/ Deletes the correspoinding &lt;see cref=\"CMSPageEntity\"\/&gt; when a page is deleted\r\n        \/\/\/ &lt;\/summary&gt;\r\n        \/\/\/ &lt;param name=\"e\"&gt;The &lt;see cref=\"EPiServer.PageEventArgs\"\/&gt; instance containing the event data.&lt;\/param&gt;\r\n        private static void RemovePageEntity(PageEventArgs e)\r\n        {\r\n            if (e == null <strong>|| e.Page == null<\/strong> || PageReference.IsNullOrEmpty(e.PageLink))\r\n            {\r\n                return;\r\n            }\r\n            \r\n            CMSPageEntity pagedataEntity = CMSPageEntityHandler.Instance.GetEntity(e.Page.PageGuid, e.Page.LanguageBranch, 0);\r\n\r\n            if (pagedataEntity == null)\r\n            {\r\n                return;\r\n            }\r\n\r\n            CMSPageEntityHandler.Instance.RemoveEntity(pagedataEntity);\r\n        }<\/pre>\n<p>And you also need to handle errors in GetClubId. I took the easy way and put a try around the code.<\/p>\n<pre class=\"lang:c# decode:true \">        private static int GetClubId(PageReference pageReference)\r\n        {\r\n            try\r\n            {\r\n                if (PageReference.IsNullOrEmpty(pageReference))\r\n                {\r\n                    return -1;\r\n                }\r\n\r\n                \/\/ Get the page\r\n                PageData page = DataFactory.Instance.GetPage(pageReference);\r\n\r\n                \/\/ Check if it is an article page, if not return\r\n                if (page is ArticlePage &amp;&amp; ((ArticlePage)page).ClubAssociation &gt; 0)\r\n                {\r\n                    return ((ArticlePage)page).ClubAssociation;\r\n                }\r\n                return -1;\r\n            }\r\n            catch { }\r\n\r\n            return -1;\r\n        }<\/pre>\n<p>Now the recycle bin should empty just fine!<\/p>\n<a href=\"https:\/\/twitter.com\/jonlin76\" class=\"twitter-follow-button\" data-show-count=\"false\" data-size=\"small\">Follow jonlin76<\/a>\n<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=\/^http:\/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+':\/\/platform.twitter.com\/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');<\/script>","protected":false},"excerpt":{"rendered":"<p>A few days ago i found i strange behaviour on our intranet site. The waste basket didn&#8217;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 &hellip; <a href=\"https:\/\/jonika.nu\/JonasBlogg\/archives\/454\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Recycle bin won&#8217;t empty when using EPiServer Relate<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[74],"tags":[118,75,119,117,120],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/jonika.nu\/JonasBlogg\/wp-json\/wp\/v2\/posts\/454"}],"collection":[{"href":"https:\/\/jonika.nu\/JonasBlogg\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jonika.nu\/JonasBlogg\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jonika.nu\/JonasBlogg\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/jonika.nu\/JonasBlogg\/wp-json\/wp\/v2\/comments?post=454"}],"version-history":[{"count":2,"href":"https:\/\/jonika.nu\/JonasBlogg\/wp-json\/wp\/v2\/posts\/454\/revisions"}],"predecessor-version":[{"id":456,"href":"https:\/\/jonika.nu\/JonasBlogg\/wp-json\/wp\/v2\/posts\/454\/revisions\/456"}],"wp:attachment":[{"href":"https:\/\/jonika.nu\/JonasBlogg\/wp-json\/wp\/v2\/media?parent=454"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jonika.nu\/JonasBlogg\/wp-json\/wp\/v2\/categories?post=454"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jonika.nu\/JonasBlogg\/wp-json\/wp\/v2\/tags?post=454"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}