EPiServer filter – part 2: create your own filter

Posted on June 28, 2009 by Frederik Vig in Code Snippet, EPiServer

If you haven’t, be sure to check out EPiServer filter – part 1.

In one of my project I had a bunch of categories and needed to filter a PageDataCollection to find pages that had one or more of the categories. Out of the box EPiServer has no filter class that does this, so I needed to create my own. This was actually pretty easy. I created a new class and implemented the EPiServer.Filters.IPageFilter interface which has three method signatures.

void Filter(EPiServer.Core.PageDataCollection pages);
void Filter(object sender, EPiServer.Filters.FilterEventArgs e);
bool ShouldFilter(EPiServer.Core.PageData page);

I then added a public property of type CategoryList for my categories. Then in my filter method I traversed the PageDataCollection and looked for matches, if there was no match the page got removed.

The complete code

public class FilterAtLeastOneCategory : IPageFilter
{
	public CategoryList Categories
	{
	    get;
	    set;
	}
 
	public FilterAtLeastOneCategory()
	{
	}
 
	public FilterAtLeastOneCategory(CategoryList categoryList)
	{
	    this.Categories = categoryList;
	}
 
	public void Filter(PageDataCollection pages)
	{
	    for (int i = pages.Count - 1; i >= 0; i--)
	    {
		bool isMatch = false;
		var categoryList = pages[i].Category;
 
		if (categoryList != null)
		{
		    foreach (var category in categoryList)
		    {
			foreach (var currentCategory in this.Categories)
			{
			    if (category == currentCategory)
			    {
				isMatch = true;
			    }
			}
		    }
		}
 
		if (!isMatch)
		{
		    pages.RemoveAt(i);
		}
	    }
	}
 
	public void Filter(object sender, FilterEventArgs e)
	{
	    this.Filter(e.Pages);
	}
 
	public bool ShouldFilter(PageData page)
	{
	    throw new NotImplementedException();
	}
}

I could then use my new filter class.

var currentCategories = CurrentPage.Category;
new FilterAtLeastOneCategory(currentCategories).Filter(myPageDataCollection);

Filter by Role

Here is another custom filter that removes all pages that the specified role doesn’t have access to.

using System;
using EPiServer.Core;
using EPiServer.Filters;
 
namespace FV.Templates.FV.Filters
{
    public class FilterRole : IPageFilter
    {
        public string RoleName
        {
            get;
            set;
        }
 
        public FilterRole()
        {
        }
 
        public FilterRole(string roleName)
        {
            this.RoleName = roleName;
        }
 
        public void Filter(PageDataCollection pages)
        {
            for (int i = pages.Count - 1; i >= 0; i--)
            {
                bool isMatch = false;
                var acl = pages[i].ACL;
 
                if (acl != null)
                {
                    foreach (var role in acl)
                    {
                        if (role.Key == this.RoleName)
                        {
                            isMatch = true;
                        }
                    }
                }
 
                if (!isMatch)
                {
                    pages.RemoveAt(i);
                }
            }
        }
 
        public void Filter(object sender, FilterEventArgs e)
        {
            this.Filter(e.Pages);
        }
 
        public bool ShouldFilter(PageData page)
        {
            throw new NotImplementedException();
        }
    }
}
new FilterRole("Everyone").Filter(pages);

Related Posts: