Select Items

buy you a drank lyrics Item Factory

The primary way of retrieving Sitecore items with Fortis is via the IItemFactory, it contains a number of methods which should suit most requirements.

http://mattmcguire.ca/category/technology Key points

As with the set up we strongly recommend using dependency injection, specifically constructor injection, and the following examples assume this.

Select Context Item

public class MyProvider
{
	private readonly IItemFactory _itemFactory;

	public MyProvider(IItemFactory itemFactory)
	{
		_itemFactory = itemFactory;
	}

	public void Run()
	{
		IItemWrapper context = _itemFactory.GetContextItem<IItemWrapper>();
	}
}

Select Single Item

IItemWrapper item = _itemFactory.Select<IItemWrapper>(/* Guid or path */);

Select Multiple Items

IEnumerable<IItemWrapper> items = _itemFactory.Select<IItemWrapper>(/* Guid, path or query */);

Select Child Items

IEnumerable<IItemWrapper> items = _itemFactory.SelectChildren<IItemWrapper>(/* Guid or path */);

Site Root Item

IItemWrapper item = _itemFactory.GetSiteRoot<IItemWrapper>();

Site Home Item

IItemWrapper item = _itemFactory.GetSiteHome<IItemWrapper>();

The item factory contains a number of methods which are not all shown here, for a full explanation of all the different methods on the item factory see the documentation.

Item Wrapper

The IItemWrapper interface also exposes methods which allow you to access items relative to the item.

Ancestor

IItemWrapper item = _itemFactory.GetContextItem<IItemWrapper>();

IItemWrapper relativeItem = item.Ancestor<IItemWrapper>();

Ancestor or self

IItemWrapper relativeItem = item.AncestorOrSelf<IItemWrapper>();

Children

IEnumerable<IItemWrapper> children = item.Children<IItemWrapper>(recursive: true);

Parent

IItemWrapper relativeItem = item.Parent<IItemWrapper>();

Parent or self

IItemWrapper relativeItem = item.ParentOrSelf<IItemWrapper>();

Siblings

IEnumerable<IItemWrapper> siblings = item.Siblings<IItemWrapper>();

Next: Renderings