Introduction
EPiServer has a powerful API for searching for pages programmatically. The idea is that you create a PropertyCriteriaCollection object containing one or more PropertyCriteria objects where each criteria represents some search condition or filter.
Searching within a specific language branch
To search in a specific language branch you can use another overload of FindPagesWithCriteria which accepts a language branch parameter. This is preferred over adding a “PageLanguageBranch” criteria to the criteria collection.
Conditional AND and OR when searching for pages
Not the Required property of the PropertyCriteria objects. If set to true the condition has to be met. If you have multiple PropertyCriteria objects with Required set to false, the search result will include all pages where any of these criterias are met. If Required is set to true, all criterias have to be met.
Find EPiServer pages programmatically
// Create criteria collection
var criterias = new PropertyCriteriaCollection
{
// Find pages of a specific page type
new PropertyCriteria()
{
Name = "PageTypeID",
Condition = CompareCondition.Equal,
Required = true,
Type = PropertyDataType.PageType,
Value = "5" // Page type ID = 5
},
// Exclude pages directly under a certain page
new PropertyCriteria()
{
Name = "PageParentLink",
Condition = CompareCondition.NotEqual,
Required = true,
Type = PropertyDataType.PageReference,
Value = "100" // Page ID = 100
},
// Find pages from this year only
new PropertyCriteria()
{
Name = "PageStartPublish",
Condition = CompareCondition.GreaterThan,
Required = true,
Type = PropertyDataType.Date,
Value = new DateTime(DateTime.Now.Year,1,1).ToString() // After January 1st
}
};
// Search pages under the start page
var pages = DataFactory.Instance.FindPagesWithCriteria(
PageReference.StartPage,
criterias);
Security note: FindPagesWithCriteria vs. FindAllPagesWithCriteria
Note the important difference between these two methods:
FindPagesWithCriteria returns all matching pages that the current user has read access for, whereas FindAllPagesWithCriteria returns all pages regardless of permissions.