Try our conversational search powered by Generative AI!

Loading...
Area: Optimizely CMS
ARCHIVED This content is retired and no longer maintained. See the latest version here.

Recommended reading 

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.

How it Works

Creating 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:

Creating a Search Criteria

Define the search criteria by creating a PropertyCriteria object that will contain the information about the property criteria for the search:

  • Set the Condition property to specify the type of comparison you want - 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, the load of a page type is based on its name, in this example PageTypeNewsItem.
  • Set the Required property to true. 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;

Creating Additional Search Criteria

Now we will define another 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
    C#
    PropertyCriteriaCollection criterias = new PropertyCriteriaCollection();
    the type of comparison you want, in this example the type is set to CompareCondition.GreaterThan.
  • Set the Name property to the PageCreated property.
  • Set the Type property to the Date property.
  • Set the Value property to specify a period of time, in this example the value is set to search for pages that have been created during the last 120 days.
  • 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;

Adding your Criteria to the Criteria Collection

Add your specified criteria to the criteria collection as follows:

C#
criterias.Add(criteria);
criterias.Add(secCriteria);

Executing the Search

Once you have completed specifying criteria you need to define where to start the search. In this example we use the 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

Filter for visitors – this 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);

The source code for the PageDataCollection and PageData classes can be found in the Framework Reference section under the Remarks section for the class.

Do you find this information helpful? Please log in to provide feedback.

Last updated: Mar 25, 2013

Recommended reading