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:
The type of match we want to perform on this filter. We have several options available Equal, GreaterThan, LessThan, NotEqual, StartsWith, EndsWith, Contained.
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.
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.
The data type of the query, this can be either a Boolean, Number, FloatNumber, PageType, PageReference, Date, String, LongString, Category, LinkCollection.
The actual data to do the match on.
Searching For Page Types
Searching For A Page Name
Searching For Page Created In The Last Month
We can build up complex queries, the next part is to actually get the results:
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.
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 organisation's backend system etc...