FindPagesWithCriteria: How to search for pages within Episerver

A common requirement in projects is to search for pages within Episerver.  One obvious example is for your websites search (When it comes to site searching you have many options like Google Site Search or EpiFind)

EPiServer provides a pretty powerful and flexible search API out of the box, FindPagesWithCriteria. In EpiWorld you can build up a search query by adding as many search criteria as as you want using a PropertyCriteria object and then passing that into FindPagesWithCriteria which returns you the matches. What you do with the returned list of pages is then up to you.

To make a query, the first thing you do is to the search criteria/filter.  To do this you use the PropertyCriteria object.  PropertyCriteria has the following properties:

  • Condition: The type of match we want to perform on this filter.  We have several options available Equal, GreaterThan, LessThan, NotEqual, StartsWith, EndsWith, Contained.
  • Name: The property/field on the PageData object you want to perform the query against.  In here I’ve gone for all pages that have a matching page type ID.
  • Required: You can think of this of a conditional OR and AND. If required is set, then the condition has to be met.  If it is not set, the result will include all pages where any of the PropertyCriteria match.
  • Type: The data type of the query, this can be either a Boolean, Number, FloatNumber, PageType, PageReference, Date, String, LongString,Category, LinkCollection.
  • Value: The actual data to do the match on.

Searching For Page Types

var pageTypeId =  ServiceLocator.Current.GetInstance<ContentTypeRepository>()
.Load<StartPage>()
.ID;
var criteria = new PropertyCriteria
{
Condition = CompareCondition.Equal,
Name = "PageTypeID",
Type = PropertyDataType.PageType,
Value = pageTypeId,
Required = true
};

Searching For A Page Name

new PropertyCriteria()
{
Name = "PageName",
Type = PropertyDataType.String,
Condition = EPiServer.Filters.CompareCondition.Equal,
Value = "Search Term"
}

Searching For Page Created In The Last Month

var criteria = new PropertyCriteria
{
Condition = EPiServer.Filters.CompareCondition.GreaterThan,
Name = "PageCreated",
Type = PropertyDataType.Date,
Value = DateTime.Now.AddMonths(-30).ToString(),
Required = true,
};

Querying Episerver

We can build up complex queries, the next part is to actually get the results:

var criterias = new PropertyCriteriaCollection
{
new PropertyCriteria()
{
Name = "PageName",
Type = PropertyDataType.String,
Condition = EPiServer.Filters.CompareCondition.Equal,
Value = _searchTerm
}
};
var repository = ServiceLocator.Current.GetInstance<IPageCriteriaQueryService>();
var pages = repository.FindPagesWithCriteria(
PageReference.StartPage,
criterias);

In the code above, we use the IPageCriteriaQueryService service to call FindPagesWithCriteria and pass in a PropertyCriteriaCollection of criteria you want to search by.

Top Tips

  • FindPagesWithCriteria only returns published pages
  • Warning FindPagesWithCriteria directly talks to the database and bypasses the cache. This means that you will get a performance hit if you are running this too many times concurrently.

FindAllPagesWithCriteria

We’ve talked about searching pages for the front-end display.  Episerver also provides an additional search method called, FindAllPagesWithCriteria.  The main difference between the two is that FindAllPagesWithCriteria ignores permissions everything.  This is a lot more dangerous approach to use as you could potentially expose pages in the front-end that shouldn’t be ‘live’, for example, you may get pages that haven’t been published returned in your search results or you may return pages that a website user does not have access to.  For these reasons, I wouldn’t recommend using this for anything that the front-end uses.

The only times I’ve really used FindAllPagesWithCriteria is for back-end admin pages for specific business requirements to enable the content editors to sell or manage the organisations backend system etc…

Jon D Jones

Software Architect, Programmer and Technologist Jon Jones is founder and CEO of London-based tech firm Digital Prompt. He has been working in the field for nearly a decade, specializing in new technologies and technical solution research in the web business. A passionate blogger by heart , speaker & consultant from England.. always on the hunt for the next challenge

More Posts

3 replies

Trackbacks & Pingbacks

  1. […] talked previously about FindPagesWithCriteria, in FindPagesWithCriteria: How to search for pages within Episerver but I will quickly cover how FindPagesWithCriteria uses the cache in the […]

  2. […] I have talked previously about the dangers of FindPagesWithCriteria. Under the hood FindPagesWithCriteria builds a bunch of SQL statements that query the database directly. There is NO caching when you use this. When you use this you need ot be careful that you are not calling it to regularly and/or returning too much data. If you are uing this a lot consider using a third party cache liek memory caching or something like Reddis to help take the load off of your database. For more advice about I recommend reading this article. […]

  3. […] talked previously about how to search for assets in the CMS using FindPagesWithCriteria: How to search for pages within EpiServer. In today’s guide we’re going to cover how to add the same functionality to your site […]

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *