This post aims to clearly illustrate a way of searching for EPiServer pages based on property values. By specifying search criteria and a place to start the search you'll get a PageDataCollection containing all matching pages.
The concept
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.
1. Create a criteria collection
The first step is to create a PropertyCriteriaCollection object which will contain the individual PropertyCriteria objects:
PropertyCriteriaCollection criterias = new
PropertyCriteriaCollection();
2. Add search criteria
Next we will define an arbitrary number of search criteria and add these to the criteria collection. First off we will add a criteria to ensure that only published pages are retrieved by checking that the PagePendingPublish property has a value of false:
PropertyCriteria pubCriteria = new PropertyCriteria();
pubCriteria.Condition =
EPiServer.Filters.CompareCondition.Equal;
pubCriteria.Name = "PagePendingPublish";
pubCriteria.Type = PropertyDataType.Boolean;
pubCriteria.Value = false.ToString();
Let's examine this. First we create a new PropertyCriteria object. We set the Condition property to specify what type of comparison we want to do. In this case we set the Condition property to CompareCondition.Equal to specify that we want to search for pages based on the criterion that a certain property value is equal to our specified value.
Next we set the Name property to specify what page property to search, PagePendingPublish in this case.
The PropertyCriteria object is not strongly typed so we need to specify the property type of the page property we're searching. The PagePendingPublish property is a boolean, so we set the Type property to PropertyDataType.Boolean.
If you're unsure about what data type to use you may find this list of page properties in EPiServer useful.
3. Add your criteria to the criteria collection
Don't forget to add your specified criteria to the criteria collection once you're done:
criterias.Add(pubCriteria);
4. Specify where to start the search
Once you're done specifying criteria you need to specify where to start the search. You could set it to your root page to search the entire site, but this may return quite a few matches (and be somewhat time-consuming). If we want to search pages that exist under the current page we could specify a PageReference like the following:
PageReference thisPage = CurrentPage.PageLink;
5. Execute the search
Getting the search result is straight-forward if you've specified your criteria correctly. The following example searches for english pages that match our criteria, exist under the current page, and are readable by the current user:
PageDataCollection matches =
DataFactory.Instance.FindPagesWithCriteria
(thisPage,
criterias,
"en",
myLanguageSelector,
EPiServer.Security.AccessLevel.Read);
We could search for all pages under the current page by simply specifying criteria and a start position like so:
PageDataCollection matches =
DataFactory.Instance.FindPagesWithCriteria
(thisPage, criterias);
There are additional overloads that can be used when calling the FindPagesWithCriteria method.
Other criteria samples
1. Searching for pages created in the last year
PropertyCriteria criteria = new PropertyCriteria();
criteria.Condition =
EPiServer.Filters.CompareCondition.GreaterThan;
criteria.Name = "PageCreated";
criteria.Type = PropertyDataType.Date;
criteria.Value = DateTime.Now.AddYears(-1).ToString();
criteria.Required = true;
criterias.Add(criteria);
2. Find pages of a certain page type
//My news item page type has ID 6
int pageTypeId = 6;
PropertyCriteria typeCriteria = new PropertyCriteria();
typeCriteria.Condition = EPiServer.Filters.CompareCondition.Equal; typeCriteria.Name = "PageTypeID";
typeCriteria.Type = PropertyDataType.PageType;
typeCriteria.Value = pageTypeId;
criterias.Add(typeCriteria);
Update: See this post for an example of how to add language branch criteria.