Blog posts by David af Sandeberg2010-05-18T10:19:34.0000000Z/blogs/David-af-Sandeberg/Optimizely WorldHiding pages in EditTree/blogs/David-af-Sandeberg/Dates/2010/5/Hiding-pages-in-EditTree/2010-05-18T10:19:34.0000000Z<p>A problem for some of our customers with globalized websites is that the EditTree gets cluttered with pages only existing in one or a few languages. </p> <p>The pages get displayed in italics and this, I take it, is a feature making it easy to get an overview of pages not having a language version in the selected language and also making it easy to create that version. This is fine when working with languages as intended by EPiServer, that is where all pages exists in all languages in a 1:1 relationship. This is however not the reality for several of our customers since they need to have different content in different market areas leading to a different page structure with different page types. </p> <p>One of the new features of CMS 6 is the ability to assign access rights on languages. While this is a great improvement like many other things in the new release I was hoping for an option to hide pages that does not exist in a language accessible by the editor. This would solve the problem for editors only editing one language. When i couldn’t find this opiton I asked the EPiServer support team if this could be achieved in any way. They pointed me to a useful <a href="/link/d5f547e9bee64442a5fa04c2c57af46d.aspx?epslanguage=en">forum post</a> that helped to get me started. </p> <p>The goal of this post is a walk through of making a httpModule that hides pages in the EditTree based on </p> <p>- Access rights on language </p> <p>- Access rights in page structure </p> <p>- The selected language in the drop down under the EditTree </p> <p> </p> <p>First, we need to define a class that inherits from IHttpModule and hook into the DataFactorys FinishedLoadingChildren event. </p> <code> <br />public class PageExcluder: IHttpModule { <br /> <br />public void Dispose() {} <br /> <br />public void Init(HttpApplication context) { <br />EPiServer.DataFactory.Instance.FinishedLoadingChildren += new EPiServer.ChildrenEventHandler(Instance_FinishedLoadingChildren); <br />} <br /> <br />void Instance_FinishedLoadingChildren(object sender, EPiServer.ChildrenEventArgs e) { <br /> <br />} <br />} <br /></code> <p> </p> <p>This method gets called each time children is loaded so we need to make sure we only hide pages when actually loading the EditTree</p> <code>if (HttpContext.Current.Request.Url.AbsolutePath.EndsWith("EditTree.aspx")) {</code> <p> </p> <p>Now we need to iterate over the pages that has been loaded from the data factory</p> <code>for (int i = 0; i < e.Children.Count; i++) {</code> <p> </p> <p>While doing this we want to remove any pages that:</p> <p>- Is not editable by the user based on access rights set on the language</p> <code>// get the languages the user has access to edit <br />StringCollection languageAccess = new StringCollection(); <br />foreach (LanguageBranch language in LanguageBranch.ListAll()) { <br />if (language.ACL.QueryDistinctAccess(EPiServer.Security.AccessLevel.Edit)) { <br />languageAccess.Add(language.LanguageID); <br />} <br />} <br />// remove pages that the user has no access to edit <br />if (!languageAccess.Contains(e.Children[i].LanguageID)) { <br />e.Children.RemoveAt(i); <br />i--; <br />continue; <br />} <br /></code> <p> </p> <p>- Is not editable by the user based on access rights set in the page structure</p> <code>if (!e.Children[i].QueryDistinctAccess(EPiServer.Security.AccessLevel.Edit)) { <br />e.Children.RemoveAt(i); <br />i--; <br />continue; <br />} <br /></code> <p> </p> <p>- Does not have a language version in the language selected by the user in the drop down under the edit tree.</p> <code>string selectedEditLang; <br />if (HttpContext.Current.Request.Cookies["editlanguagebranch"] != null) <br />selectedEditLang = HttpContext.Current.Request.Cookies["editlanguagebranch"].Value; <br />else <br />selectedEditLang = CultureInfo.CurrentUICulture.TwoLetterISOLanguageName; <br /> <br />if (e.Children[i].LanguageID != selectedEditLang) { <br />e.Children.RemoveAt(i); <br />i--; <br />continue; <br />} <br /></code> <p> </p> <p>In the complete version of this module you can specify which one(s) of these checks you want to do by adding entries to your appSettings. You can download the complete project <a href="http://demo.two.se/EPiServerfPageTreeModule.zip">here</a>.</p> <p> </p> <p>This is my first blog post here on EPiServer world and i hope you found it useful. I appreciate any feedback, improvements or suggestions.</p>