Skip to content

Querying Content

James Jackson-South edited this page Mar 9, 2017 · 2 revisions

ContentHelper

The ContentHelper class provides a unified way to traverse the published document tree in a strong typed manner. It is present in the ZoombracoController, ZoombracoApiController and ZoombracoViewPage<T> classes bound to the UmbracoHelper present in each class. The ContentHelper is also used for the navigation methods within the base models.

API

ContentHelper provides a simple but immensely powerful API for retrieving published content. All the generic types can accept base classes and interfaces as well as concrete classes allowing polymorphic querying of strong types.

/// <summary>
/// Gets the underlying <see cref="UmbracoHelper"/> for querying published content or media.
/// </summary>
public UmbracoHelper UmbracoHelper { get; }

/// <summary>
/// Registers the given type to allow conversion. All types inheriting Page, Component, or NestedComponent
/// are automatically registered.
/// </summary>
/// <param name="type">The type to register.</param>
/// <param name="alias">Any alias for the given type.</param>
public static void RegisterType(Type type, string alias = null)

/// <summary>
/// Gets the node matching the given id.
/// </summary>
/// <param name="nodeId">The id of the node to return.</param>
/// <returns>
/// The <see cref="Page"/> matching the id.
/// </returns>
public Page GetById(int nodeId)

/// <summary>
/// Gets the node matching the given id.
/// </summary>
/// <typeparam name="T">The type to return</typeparam>
/// <param name="nodeId">The id of the node to return.</param>
/// <returns>
/// The given <see cref="Type"/> instance matching the id.
/// </returns>
public T GetById<T>(int nodeId)

/// <summary>
/// Gets the nodes matching the given <see cref="Type"/>.
/// </summary>
/// <typeparam name="T">The type to return</typeparam>
/// <param name="rootId">
/// The id of the root node to start from. If not given the method will return
/// all nodes matching the type.
/// </param>
/// <param name="level">The level to search.</param>
/// <returns>
/// The nodes matching the given <see cref="Type"/>.
/// </returns>
public IEnumerable<T> GetByNode<T>(int rootId = 0, int level = int.MaxValue)

/// <summary>
/// Gets the root nodes as instances of <see cref="Page"/>.
/// </summary>
/// <returns>
/// The root nodes as instances of <see cref="IEnumerable{Page}"/>.
/// </returns>
public IEnumerable<Page> GetRootNodes()

/// <summary>
/// Gets the root nodes matching the given type.
/// </summary>
/// <typeparam name="T">The type to return</typeparam>
/// <returns>
/// The root nodes matching the given <see cref="Type"/>.
/// </returns>
public IEnumerable<T> GetRootNodes<T>()

/// <summary>
/// Gets the first root node in the current site as an instance of <see cref="Page"/>.
/// </summary>
/// <returns>
/// The root node as an instance of <see cref="Page"/>.
/// </returns>
public Page GetRootNode()

/// <summary>
/// Gets the first root node in the current site matching the given type.
/// </summary>
/// <typeparam name="T">The type to return</typeparam>
/// <returns>
/// The root node matching the given <see cref="Type"/>.
/// </returns>
public T GetRootNode<T>()

/// <summary>
/// Gets the parent of the current instance as an instance of <see cref="Page"/>.
/// </summary>
/// <param name="nodeId">The id of the current node to search from.</param>
/// <returns>
/// The parent as an instance of <see cref="Page"/>.
/// </returns>
public Page GetParent(int nodeId)

/// <summary>
/// Gets the parent of the current instance as the given <see cref="Type"/>.
/// </summary>
/// <typeparam name="T">The type to return</typeparam>
/// <param name="nodeId">The id of the current node to search from.</param>
/// <returns>
/// The parent as an instance of the given <see cref="Type"/>.
/// </returns>
public T GetParent<T>(int nodeId)

/// <summary>
/// Gets the ancestors of the current instance as an <see cref="IEnumerable{Page}"/>.
/// </summary>
/// <param name="nodeId">The id of the current node to search from.</param>
/// <param name="maxLevel">The maximum level to search.</param>
/// <returns>
/// The <see cref="IEnumerable{Page}"/>.
/// </returns>
public IEnumerable<Page> GetAncestors(int nodeId, int maxLevel = int.MaxValue)

/// <summary>
/// Gets the ancestors of the current instance as the given <see cref="Type"/>.
/// </summary>
/// <typeparam name="T">The type to return</typeparam>
/// <param name="nodeId">The id of the current node to search from.</param>
/// <param name="maxLevel">The maximum level to search.</param>
/// <returns>
/// The <see cref="IEnumerable{T}"/>.
/// </returns>
public IEnumerable<T> GetAncestors<T>(int nodeId, int maxLevel = int.MaxValue)

/// <summary>
/// Gets the children of the current instance as an <see cref="IEnumerable{Page}"/>.
/// </summary>
/// <param name="nodeId">The id of the current node to search from.</param>
/// <returns>
/// The <see cref="IEnumerable{Page}"/>.
/// </returns>
public IEnumerable<Page> GetChildren(int nodeId)

/// <summary>
/// Gets the children of the current instance as the given <see cref="Type"/>.
/// </summary>
/// <typeparam name="T">The type to return</typeparam>
/// <param name="nodeId">The id of the current node to search from.</param>
/// <returns>
/// The <see cref="IEnumerable{T}"/>.
/// </returns>
public IEnumerable<T> GetChildren<T>(int nodeId)

/// <summary>
/// Gets the descendants of the current instance as an <see cref="IEnumerable{Page}"/>.
/// </summary>
/// <param name="nodeId">The id of the current node to search from.</param>
/// <param name="level">The level to search.</param>
/// <returns>
/// The <see cref="IEnumerable{Page}"/>.
/// </returns>
public IEnumerable<Page> GetDescendants(int nodeId, int level = 0)

/// <summary>
/// Gets the descendants of the current instance as the given <see cref="Type"/>.
/// </summary>
/// <typeparam name="T">The type to return</typeparam>
/// <param name="nodeId">The id of the current node to search from.</param>
/// <param name="level">The level to search.</param>
/// <returns>
/// The <see cref="IEnumerable{T}"/>.
/// </returns>
public IEnumerable<T> GetDescendants<T>(int nodeId, int level = 0)

/// <summary>
/// Gets the list of stored types.
/// </summary>
/// <returns>
/// The <see cref="IEnumerable{Type}"/> containing the registered types.
/// </returns>
public IEnumerable<Type> GetRegisteredTypes()

/// <summary>
/// Gets the stored type matching the given name.
/// </summary>
/// <param name="name">The name of the type to retrieve.</param>
/// <returns>
/// The stored <see cref="Type"/>.
/// </returns>
public Type GetRegisteredType(string name)
Clone this wiki locally