Documentation Center

Filter example

You can create a filter to query the Content Data Store based on certain criteria. You can organize criteria into a tree structure which Content Delivery then turns into a database query string.

ASP.NET example

The following code sample demonstrates how to filter out Components based on a Schema with ID 511 or 34. The result set returned is a set of strings (rather than objects), limited to a maximum of 5 results and sorted by the title of the Component.

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(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

The following code sample demonstrates how to filter out Components based on a Schema with ID 511 or 34. The result set returned is a set of strings (rather than objects), limited to a maximum of 5 results and sorted by the title of the Component.

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(allCriteria);

		// 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();
	}
}