Select Items

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.

Key points

  • Always pass in the interface representing the template and not the concrete class
  • You can request an item from Sitecore as IItemWrapper whether or not any of its templates are mapped. This is useful if you don’t know (or don’t need to know) what template an item is and perhaps just need basic information such as the item name.
  • The item factory methods will return null if either
    • The item requested is not found
    • The item requested does not map to the interface (template) specified

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