Tags

, , ,

Sitecore Queries are useful for getting items from the Sitecore database. A lot of logic can be integrated in the query as well. Here follows a method that could be used for getting a specific number of items from the database:

public Item[] GetProducts(int numberOfItems)
{
	string query = string.Format("/sitecore/content/products/*[position() <= {0}]", numberOfItems);

	Item[] items = Sitecore.Context.Database.SelectItems(query);

	return items;
}

The function “position()” in the query returns the position of the item. This position is the absolute position of the item in the tree. So do not use this function in conjunction with other filters in the query, like “@@templatename = ‘MyTemplateName'”.

Advertisements