In this tutorial, you will learn how to use the Episerver FindPagesWithCriteria()
API call to search for content pages within Episerver CMS. When it comes to site searching you have many advanced search options like Google Site Search or EpiFind). If you just want to do a simple content search, Episerver provides a pretty powerful and flexible search API out-of-the-box.
Using FindPagesWithCriteria
you can build up a search query by adding as many search criteria as as you want. This is done using PropertyCriteria
objects and passing these objects as parameters into FindPagesWithCriteria
. If this sound something of interest to you, read on 🔥🔥🔥
Building A Search Query
To make a query, the first thing you do is to define search criteria and add any filtering you require. To do this you use PropertyCriteria
. When it comes PropertyCriteria
for building your search you have several options:
Condition: The type of match we want to perform on this filter. We have several options available Equal
, GreaterThan
, LessThan
, NotEqual
, StartsWith
, EndsWith
, and Contained
.
Name: The property/field on the PageData
object you want to target. One common use-case would be to search based on the ID
Required: You can think of this as either a conditional OR
or an AND
. If required is set, 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, can be either a Boolean
, Number
, FloatNumber
, PageType
, PageReference
, Date
, String
, LongString
, Category
, or LinkCollection
.
Value: The search term
Creating Search Queries
Now we know the options, let's build some example queries:
Searching For Page Types: Search by page-type:
Searching For A Page Name: Search by page-name
Searching For Page Created In The Last Month: Search by date:
Give Me Results!
After you build a query, the next part is to actually get the results. This is done using this code:
This code uses the IPageCriteriaQueryService
service to call FindPagesWithCriteria
. The PropertyCriteriaCollection
we created is passed in and then Episerver performs its magic 🪄. When using FindPagesWithCriteria
I recommend keeping these points in mind:
FindPagesWithCriteria
only returns published pagesFindPagesWithCriteria
directly queries the database and it does not use the cache. This means that you will get a performance hit if you are run this query too often in production. This could take the site down, so be warned!
FindAllPagesWithCriteria
Episerver also provides an additional search method called, FindAllPagesWithCriteria
. The main difference between FindAllPagesWithCriteria
and FindPagesWithCriteria
, is that FindAllPagesWithCriteria
ignores permissions and published status. Using this on the front end is dangerous, as you could potentially expose pages in the front end that shouldn't be 'live'. Using this API can be useful for backend admin modules though 😉
Happy Coding 🤘