EPiServer Extension methods, part 1 of many

November 7, 2009

So Frederik Vig started a community project around EPiServer Extensions which I believe is a great initiative. Already having a great number of extension methods that we are currently using.

Rather than just dump them in the source code repository I thought I would try to get my blogging going again by posting each method with a short blog post so that people can comment easy and come with suggestions for improvement

So lets get on to it, here is the first set, a few methods for traversing up in the page structure, apologies for the bad code formatting.

Edit: Sorted out the source code highlighting…

/// <summary>
/// Gets the parent page data.
/// The method will return null if no parent is found
/// or if the page is in the recycle bin.
/// </summary>
/// <param name="page">The page subject.</param>
/// <returns>
/// The parent page data or null if the parent is in the recycle bin.
/// </returns>
public static PageData GetParent(this PageData page)
{
  if (page != null && !PageReference.IsNullOrEmpty(page.ParentLink) && !DataFactory.Instance.IsWastebasket(page.PageLink))
  {
    return page.ParentLink.GetPage();
  }
  return null;
}

/// <summary>
/// Gets all the ancestors of a page.
/// </summary>
/// <param name="page">The page subject.</param>
/// <returns>An enumerable list of ancestor pages.</returns>
public static IEnumerable<PageData> GetAncestors(this PageData page)
{
  return page.GetAncestors(PageReference.RootPage);
}

/// <summary>
/// Gets all the ancestors of a page up to a given root page.
/// </summary>
/// <param name="page">The page subject.</param>
/// <param name="rootPage">The root page.</param>
/// <returns>An enumerable list of ancestor pages.</returns>
public static IEnumerable<PageData> GetAncestors(this PageData page,
   PageReference rootPage)
{
  PageData parent = page.GetParent();

  if (parent != null)
  {
    yield return parent;

    // Stop at given root page
    if (!parent.PageLink.IsEqual(rootPage, true))
    {
      foreach (PageData ancestor in parent.GetAncestors(rootPage))
      {
        yield return ancestor;
      }
    }
  }
}

/// <summary>
/// Gets the ancestor at a given level from a root page.
/// If the page provided is at this level, it is returned.
/// This method is especially useful when finding the root
/// page for a secondary navigation structure.
/// </summary>
/// <param name="page">The page the defines what branch to look through.</param>
/// <param name="rootPage">The root page where the level is calculated from.</param>
/// <param name="offset">The level offset.</param>
/// <returns>
/// PageData of the ancestor at the given level or
/// null if there isn't any pages at that level.
/// </returns>
public static PageData GetAncestorAtLevel(this PageData page,
  PageReference rootPage, int offset)
{
  if (page == null)
    throw new ArgumentNullException("page");

  if (PageReference.IsNullOrEmpty(rootPage))
    throw new System.ArgumentException("rootPage");

  if (offset < 0)
    throw new ArgumentOutOfRangeException("offset", "Offset cannot be negative");

  List<PageData> ancestors = page.GetAncestors(rootPage).Reverse().ToList();
  if (ancestors.Count > 0 && ancestors[0].PageLink.CompareToIgnoreWorkID(rootPage))
  {
    // If the offset equals number of ancestors,
    // the current page is at the offset spot
    if (ancestors.Count == offset)
    {
      return page;
    }

    if (ancestors.Count > offset)
    {
      return ancestors[offset];
    }
  }
  return null;
}
Advertisements

4 Responses to “EPiServer Extension methods, part 1 of many”


  1. […] This post was mentioned on Twitter by Frederik Vig, Henrik Nystrom. Henrik Nystrom said: blogged: EPiServer Extension methods, part 1: http://bit.ly/3AEw9f […]

  2. Viktor Says:

    Great initiative.

    You should install syntaxhightlighter evolved, gives you great code formatting imho :)

  3. Ullmark Says:

    Hello,
    I also made my own EPiServer extensions. Since we have worked together some of the function names may be the same (weird ;-)) But they are created from scratch and… a bit hasty.

    http://github.com/ullmark/cs-extensions


  4. […] post: EPiServer Extension methods, part 1 of many […]


Comments are closed.

%d bloggers like this: