Creating a filter

Create a filter to query the Content Data Store based on a number of criteria. The API lets you organize these criteria in a tree structure, which Content Delivery then turns into a database query string.

The following code samples demonstrates how to filter out only those items that meet the following criteria:

  • The item is based on a Schema with an ID of either 511 or 34
  • The item is a Component

The set of results is returned as a set of strings rather than objects, is to be limited to a maximum of 5 results, and is sorted by the title of the Component.

ASP.NET example:

using Tridion.ContentDelivery.DynamicContent;
using Tridion.ContentDelivery.DynamicContent.Query;

public void FilterTestComponents()
{
  try
  {
    // Schema has ID of either 511 (Article) or 34 (Press Release).
    ItemSchemaCriteria IsArticle = new ItemSchemaCriteria(511);
    ItemSchemaCriteria IsPressRelease = new ItemSchemaCriteria(34);
    Criteria IsArticleOrPressRelease = CriteriaFactory.Or(IsArticle, IsPressRelease);

    // Type of the item is 16 (Component).
    ItemTypeCriteria IsComponent = new ItemTypeCriteria(16);

    // Both of the above conditions must be true
    Criteria AllCriteria = CriteriaFactory.And(IsArticleOrPressRelease, IsComponent);

    // Add these criteria to a query
    Query MyQuery = new Query();
    MyQuery.SetCriteria(AllCriteria);

    // Limit the results to max 5, and sort them ascending on title.
    MyQuery.AddLimitFilter(new LimitFilter(5));
    SortParameter sortParameter = new SortParameter(SortParameter.ItemTitle, SortParameter.Ascending);
    MyQuery.AddSorting(sortParameter);

    // Run the query and get the results
    String[] ItemResults = MyQuery.ExecuteQuery();
  }
  catch (Exception e)
  {
    Console.WriteLine(e.Message);
  }
}

JSP example:

import com.tridion.broker.querying.CriteriaFactory;
import com.tridion.broker.querying.Query;
import com.tridion.broker.querying.criteria.Criteria;
import com.tridion.broker.querying.criteria.content.ItemSchemaCriteria;
import com.tridion.broker.querying.criteria.content.ItemTypeCriteria;
import com.tridion.broker.querying.filter.LimitFilter;
import com.tridion.broker.querying.sorting.SortParameter;
import com.tridion.broker.StorageException;
 
public void filterTestComponents() 
{
  try {
    CriteriaFactory criteriaFactory = new CriteriaFactory();
  
    // Schema has ID of either 511 (Article) or 34 (Press Release).
    ItemSchemaCriteria isArticle = new ItemSchemaCriteria(511);
    ItemSchemaCriteria isPressRelease= new ItemSchemaCriteria(34);
    Criteria isArticleOrPressRelease = CriteriaFactory.Or(isArticle, isPressRelease);
    
    // Type of the item is 16 (Component).
    ItemTypeCriteria isComponent = new ItemTypeCriteria(16);
    
    // Both of the above conditions must be true
    Criteria allCriteria = CriteriaFactory.And(isArticleOrPressRelease, isComponent);
    
    // Add these criteria to a query
    Query query = new Query();         
    query.setCriteria(searchCriteria);

    // Limit the results to max 5, and sort them ascending on title.
    query.addLimitFilter(new LimitFilter(5));
    SortParameter sortParameter = new SortParameter(SortParameter.ITEMS_TITLE, SortParameter.ASCENDING);
    query.addSorting(sortParameter);
    
    // Run the query and get the results
    String[] itemResults = query.executeQuery();
	} catch(StorageException e) {
	  e.printStackTrace();
  }
}