Introduction

Here we illustrate a way of searching for EPiServer pages created within the last 7 days. 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 to search for pages created recently

Create a criteria collection

The first step is to create a PropertyCriteria object which will contain the information about the property criteria for the search:

CopyC#
PropertyCriteriaCollection criterias = new PropertyCriteriaCollection();

The next step is to create a PropertyCriteriaCollection object which will contain the individual PropertyCriteria objects:

CopyC#
PropertyCriteria propCriteria = new PropertyCriteria();

Add search criteria

Now we will define the search criteria and add these to the criteria collection. We add criteria to ensure that only pages created over the last 7 days are retrieved:

CopyC#
propCriteria.Condition = EPiServer.Filters.CompareCondition.GreaterThan;
propCriteria.Name = "PageCreated";
propCriteria.Type = PropertyDataType.Date;
propCriteria.Value = DateTime.Now.AddYears(-1).ToString();
propCriteria.Required = true;
criterias.Add(criteria);

Add your criteria to the criteria collection

Add your specified criteria to the criteria collection:

CopyC#
criterias.Add(pubCriteria);

Specify where to start the search

Once you have completed specifying criteria you need to specify where to start the search. You could set it to your start page to search the entire site.

CopyC#
PageReference parent = PageReference.StartPage;

Please note that this may return quite a few matches (and in some cases could be time-consuming). If we want to search pages that exist under the current page you may want to specify a PageReference:

CopyC#
PageReference thisPage = CurrentPage.PageLink;

Execute the search

Getting the search result is straight-forward if you've specified your criteria correctly. The following example searches for pages that match our criteria under the page specified earlier:

CopyC#
PageDataCollection matches = DataFactory.Instance.FindPagesWithCriteria(thisPage, criterias);

Display the results

List the search results - all the pages found along with page name, Type and ID.

CopyC#
foreach (PageData page in matches)
{   
  Response.Write(String.Format("\n\nFound page: {0} - {1} ({2})\n",
     page.PageLink.ID,
     page.PageName,
     page.PageTypeName));
}