Filtering Content

A very common operation is filtering of lists. To simplify this there is a concept of filters. These are the filters available by default:

Filter TypeDescription
AccessFilterremoves items not authorized by the user
CompositeFilteris composed of other filters, removes items not authorized by all of these
NavigationFilteris composed of a page, visible, published and access filter
CountFilterskip and takes a number of items, needs to be reset after each use
DelegateFilterencapsulates an action that does the filtering
DuplicateFilterremoves duplicates, needs to be reset after each use
InverseFiltercontains another filter which behavior is inversed
NullFilterdoesn't filter
PageFilterallows only pages (not parts)
ParentFilterremoves anything not structured below a parent
PublishedFilterremoves unpublished and expired items
TypeFilterremoves items not deriving from a certain type
VisibleFilterremoves non-visible items
ZoneFilterremoves items not in a given zone

Examples

Example, filtering found items:

     private IEnumerable<ContentItem> FindRecentlyPublishedWithFinder()
     {
           var recentlyPublishedItems = N2.Find.Items
                .Where.Published.Gt(DateTime.Now.AddMonths(-1))
                .Filters(new AccessFilter())
                .Select();
           return recentlyPublishedItems;
     }

The GetChildren method without parameters uses AccessFilter. When filtering children remember to include an access filter. Example, filtering out child pages that are accessible:

     public ActionResult FilteringChildPages()
     {
           ViewData["childPages"] = CurrentPage.GetChildren(
                new CompositeFilter(
                      new AccessFilter(),
                      new PageFilter()));
           return View();
     }

The filter can also be invoked directly. Calling filter will remove entries from the list.

     public ActionResult FilteringLists()
     {
           var children = CurrentPage.GetChildren();
           var filter = new PublishedFilter();
           // filtering a list will remove entries from the list
           filter.Filter(children);
 
           return View();
     }

Without changing the list, it’s possible to call Pipe which returns the matching items.

     public ActionResult PipingEnumerations()
     {
           var children = CurrentPage.GetChildren();
           var filter = new PublishedFilter();
           // piping will return a filtered enumeration without changing the input
           var filteredChildren = filter.Pipe(children);
 
           return View();
     }