Search for pages based on page type
Describes how to search for pages based on page type.
Optimizely's recommended search functionality is Optimizely Search & Navigation (formerly named Optimizely Find), a cloud-ready extension that lets you build advanced customized search features based on your content model.
You can programmatically search for Optimizely Content Management System (CMS) pages based on a certain page type using built-in functionality for non-cloud environments. By specifying search criteria and a place to start the search, you get 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.
Create a criteria collection
-
Create a
PropertyCriteriaCollectionobject that contains the individualPropertyCriteriaobjects:PropertyCriteriaCollection criterias = new PropertyCriteriaCollection(); -
Create a
PropertyCriteriaobject that contains the information about the property criteria for the search.
Create a search criteria
Define the search criteria by creating a PropertyCriteria object that contains the information about the property criteria for the search:
- Set the
Conditionproperty to specify the type of comparison you want; for example,CompareCondition.Equal. - Set the
Nameproperty to thePageTypeIDproperty. - Set the
Typeproperty to thePageTypeproperty. - Set the
Valueproperty to specify what page type to search for (the load of a page type is based on its name); for example,PageTypeNewsItem. - Set the
Requiredproperty to true. This criterion is required for a match.
PropertyCriteria criteria = new PropertyCriteria();
criteria.Condition = CompareCondition.Equal;
criteria.Name = "PageTypeID";
criteria.Type = PropertyDataType.PageType;
criteria.Value = _contentTypeRepository.Load("PageTypeNewsItem").ID.ToString();
criteria.Required = true;In this example the IPageCriteriaQueryService and IContentTypeRepositoryinterfaces are injected through Dependency Injection in the constructor of the calling class, for example, like below. The service is used to perform the search.
private readonly IPageCriteriaQueryService _pageCriteriaQueryService;
private readonly IContentTypeRepository _contentTypeRepository;
public CustomContentLocator(IPageCriteriaQueryService pageCriteriaQueryService, IContentTypeRepository contentTypeRepository) {
_pageCriteriaQueryService = pageCriteriaQueryService;
_contentTypeRepository = contentTypeRepository;
}Create additional search criteria
Define another search criterion by creating a PropertyCriteria object that contains the information about the property criteria for the search:
-
Set the
Conditionproperty to specify the type of comparison you want; for example,CompareCondition.GreaterThan.PropertyCriteriaCollection criterias = new PropertyCriteriaCollection(); -
Set the
Nameproperty to thePageCreatedproperty. -
Set the
Typeproperty to theDateproperty. -
Set the
Valueproperty to specify a period of time; for example, set it to search for pages that were created during the last 120 days. -
Set the
Requiredproperty to true, this criterion is required for a match.
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 criteria to the criteria collection
Add your specified criteria to the criteria collection as follows:
criterias.Add(criteria);
criterias.Add(secCriteria);Execute the search
After you specify the criteria, define where to start the search. Use the start page as the search starting point.
NoteThe following example may return many matches (and in some cases could be time-consuming).
PageDataCollection _newsPageItems = _pageCriteriaQueryService.FindPagesWithCriteria(PageReference.StartPage, criterias);FilterForVisitor
The following filter removes pages that the current user does not have access to or that are not currently published:
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.
Updated 7 days ago