Introduction

In this How To we shall demonstrate how to programmatically search for EPiServer pages based on a certain page type.

By specifying search criteria and a place to start the search you will get a PageDataCollection containing all matching pages.

The DataFactory contains a method called FindPagesWithCriteria which takes a PageReference (representing where to start the search) and a PropertyCriteriaCollection object (representing what to search for).

When the FindPagesWithCriteria method is called it returns a PageDataCollection object containing matching pages

How to search for pages based on page type

Create a criteria collection

The first step is to create a PropertyCriteriaCollection object which will contain the individual PropertyCriteria objects:

CopyC#
PropertyCriteriaCollection criterias = new PropertyCriteriaCollection();

The next step is to create a PropertyCriteria object which will contain the information about the property criteria for the search:

Create search criteria

Now we will define the search criteria. The first step is to create a PropertyCriteria object which will contain the information about the property criteria for the search:

CopyC#
PropertyCriteria criteria = new PropertyCriteria();
criteria.Condition = CompareCondition.Equal;
criteria.Name = "PageTypeID";
criteria.Type = PropertyDataType.PageType;
criteria.Value = PageType.Load("PageTypeNewsItem").ID.ToString();
criteria.Required = true;

Create more search criteria

Now we will define another the search criteria. The first step is to create a PropertyCriteria object which will contain the information about the property criteria for the search:

CopyC#
PropertyCriteria secCriteria = new PropertyCriteria();
secCriteria.Condition = EPiServer.Filters.CompareCondition.GreaterThan;
secCriteria.Name = "PageCreated";
secCriteria.Type = PropertyDataType.Date;
secCriteria.Value = DateTime.Now.AddDays(-120).ToString();
secCriteria.Required = true;

Add your criteria to the criteria collection

Add your specified criteria to the criteria collection:

CopyC#
criterias.Add(criteria);
criterias.Add(secCriteria);

Execute the search

Once you have completed specifying criteria you need to specify where to start the search. In this example we use start page as the search starting point. Please note that this may return quite a few matches (and in some cases could be time-consuming):

CopyC#
PageDataCollection _newsPageItems = DataFactory.Instance.FindPagesWithCriteria(PageReference.StartPage, criterias);

FilterForVisitor

We filter for visitors - which is a filter that removes any pages that the current user does not have access to or that are not currently published:

CopyC#
FilterForVisitor.Filter(_newsPageItems);
new FilterSort(FilterSortOrder.PublishedDescending).Filter(_newsPageItems);

Note! The source code for the PageDataCollection and PageData classes can now viewed in the Framework Reference section of the EPiServer CMS SDK - the source code is displayed under the Remarks section for the class.