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

Recommended reading 

You can programmatically search for Episerver CMS pages based on a certain page type. By specifying search criteria and a place to start the search you gets a PageDataCollection containing 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

  1. Create a PropertyCriteriaCollection object that contains the individual PropertyCriteria objects:
    C#
    PropertyCriteriaCollection criterias = new PropertyCriteriaCollection();
  2. Create a PropertyCriteria object that contains the information about the property criteria for the search:

Creating a search criteria

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

  1. Set the Condition property to specify the type of comparison you want; for example, CompareCondition.Equal.
  2. Set the Name property to the PageTypeID property.
  3. Set the Type property to the PageType property.
  4. Set the Value property to specify what page type to search for; (the load of a page type is based on its name); for example, PageTypeNewsItem.
  5. 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

Define another search criteria by creatihng a PropertyCriteria object tghat contains the information about the property criteria for the search:

  1. Set the Condition property to specify the type of comparison you want; for example, CompareCondition.GreaterThan.
    C#
    PropertyCriteriaCollection criterias = new PropertyCriteriaCollection();
  2. Set the Name property to the PageCreated property.
  3. Set the Type property to the Date property.
  4. Set the Value property to specify a period of time; for example, set to search for pages that are created during the last 120 days.
  5. 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

After you specify criteria,  define where to start the search. Use the start page as the search starting point.

Note: The following example 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. The following filter removes 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);

You can find the source code for the PageDataCollection and PageData classes 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: Sep 21, 2015

Recommended reading