Five New Optimizely Certifications are Here! Validate your expertise and advance your career with our latest certification exams. Click here to find out more
Five New Optimizely Certifications are Here! Validate your expertise and advance your career with our latest certification exams. Click here to find out more
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.
The first step is to create a PropertyCriteriaCollection object which will contain the individual PropertyCriteria objects:
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:
Define the search criteria by creating a PropertyCriteria object that will contain the information about the property criteria for the search:
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;
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:
PropertyCriteriaCollection criterias = new PropertyCriteriaCollection();
the type of comparison you want, in this example the type is set to CompareCondition.GreaterThan.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 specified criteria to the criteria collection as follows:
criterias.Add(criteria);
criterias.Add(secCriteria);
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):
PageDataCollection _newsPageItems = Locate.PageCriteriaQueryService().FindPagesWithCriteria(PageReference.StartPage, criterias);
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:
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.
Last updated: Mar 25, 2013