Table of Contents
Introduction
This document describes how to programmatically search for EPiServer CMS pages based on a certain page type. By specifying search criteria and a place to start the search you will get a PageDataCollection containing all matching pages.
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.
Searching for Pages Based on Page Type
Create a Criteria Collection
The first step is to create a PropertyCriteriaCollection object which will contain the individual PropertyCriteria objects:
C#
PropertyCriteriaCollection criterias = new PropertyCriteriaCollection();
The next step is to create a PropertyCriteria object which will contain the information about the property criteria for the search:
Create Search Criteria
Now we will define the search criteria. The first step is to create a PropertyCriteria object which will contain the information about the property criteria for the search:
- Set the Condition property to specify what type of comparison we want to do – in this example we set it to CompareCondition.Equal.
- Set the Name property to the PageTypeID property.
- Set the Type property to the PageType property.
- Set the Value property to specify what page type to search for, we load a page type based on its name – PageTypeNewsItem in this example.
- Set the Required property to true &ndash, this criteria is required for a match.
C#
PropertyCriteria criteria = new PropertyCriteria();
criteria.Condition = CompareCondition.Equal;
criteria.Name = "PageTypeID";
criteria.Type = PropertyDataType.PageType;
criteria.Value = Locate.ContentTypeRepository().Load("PageTypeNewsItem").ID.ToString();
criteria.Required = true;
Create More Search Criteria
Now we will define another the search criteria. The first step is to create a PropertyCriteria object which will contain the information about the property criteria for the search:
- Set the Condition property to specify what type of comparison we want to do – in this example we set it to CompareCondition.GreaterThan.
- Set the Name property to the PageStartPublish property.
- Set the Type property to the Date property.
- Set the Value property to specify what page type to search for, we search for pages created since the beginning of 2009.
- Set the Required property to true – this criteria is required for a match.
C#
PropertyCriteria secCriteria = new PropertyCriteria();
secCriteria.Condition = EPiServer.Filters.CompareCondition.GreaterThan;
secCriteria.Name = "PageCreated";
secCriteria.Type = PropertyDataType.Date;
secCriteria.Value = DateTime.Now.AddDays(-120).ToString();
secCriteria.Required = true;
Add your Criteria to the Criteria Collection
Add your specified criteria to the criteria collection:
C#
criterias.Add(criteria);
criterias.Add(secCriteria);
Execute the Search
Once you have completed specifying criteria you need to specify where to start the search. In this example we use start page as the search starting point. Please note that this may return quite a few matches (and in some cases could be time-consuming):
C#
PageDataCollection _newsPageItems = Locate.PageCriteriaQueryService().FindPagesWithCriteria(PageReference.StartPage, criterias);
FilterForVisitor
We filter for visitors – which is a filter that removes any pages that the current user does not have access to or that are not currently published:
C#
FilterForVisitor.Filter(_newsPageItems);
new FilterSort(FilterSortOrder.PublishedDescending).Filter(_newsPageItems);
Note the source code for the PageDataCollection and PageData classes can be viewed in the Framework Reference section of the EPiServer CMS SDK - the source code is displayed under the Remarks section for the class.
Do you find this information helpful? Please log in to provide feedback.