Developer sitting behind two large monitors.

Hide pages in the page tree in EPiServer 7

This article was migrated from an older iteration of our website, and it could deviate in design and functionality.


On a large site with alot of editors, you can assist them by hiding parts of the page tree they don't have access to.

Estimated read time : 2 minutes

Jump to

Key takeaways

  • You can hide pages conditionally through code
  • Easy to setup using dependency injection

Introduction

Sometimes it could be useful to manipulate how certain pages are displayed in the page tree in EPiServer 7. Because the page tree is loaded from a REST based store, all you really need to do is provide a custom implementation of the ContentQuery responsible for loading the children.

The REST stores

The REST stores in EPiServer are discovered at application startup. The ContentStructureStore which is used for loading content into the page tree, will be provided with a number of ContentQuery classes at initialization. One of them is EPiServer.Cms.Shell.UI.Rest.ContentQuery.GetChildrenQuery. I'll show you how to manipulate this.

Provide your custom query

First add a reference to EPiServer.Cms.Shell.UI assembly. Yes, you will now have a dependency to an EPiServer module, so think about that for a moment.

Then add your own GetChildrenQuery class. You get alot of good stuff through the ContentQueryParameters object passed to GetContent, such as CurrentPrincipal, CurrentLanguage etc.

So, short example. I’ll hide all pages of the type DivisionPage that the editor doesn’t have permission to edit.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Web;
using EPiServer.Cms.Shell.UI.Rest.ContentQuery;
using EPiServer.Core;
using EPiServer.Filters;
using EPiServer.Security;
using EPiServer.ServiceLocation;
using EPiServer.Templates.Alloy.Models.Pages;

namespace EPiServer.Templates.Alloy.Business
{
    [ServiceConfiguration(typeof(IContentQuery))]
    public class MyGetChildrenQuery : GetChildrenQuery
    {
        public MyGetChildrenQuery(IContentQueryHelper queryHelper, IContentRepository contentRepository, LanguageSelectorFactory languageSelectorFactory) : base(queryHelper, contentRepository, languageSelectorFactory)
        {
        }

        protected override IEnumerable<IContent> GetContent(ContentQueryParameters parameters)
        {
            return base.GetContent(parameters)
                       .Where(x => !(x is DivisionPage) || FilterAccess.QueryDistinctAccessEdit(x, AccessLevel.Edit));
        }
    }
}