Posted in: Comments

Sometimes you don't want to use dynamic properties but still needs to find a certain page with a property or a page with a specific pagetype id.

Here's my take on it.

These functions probably needs some error handling, but they still shows the concept.

/// <summary>
/// Travers to the nearest parent with the specified PageTypeID
/// </summary>
public static PageData TraverseTo(this PageData page, int pageTypeID)
{
    return GlobalFunctions.TraverseTo(page.PageLink, pageTypeID);
}

/// <summary>
/// Travers to the nearest parent with one of the specified PageTypeID's
/// </summary>
public static PageData TraverseTo(this PageData page, int[] pageTypeID)
{
    return GlobalFunctions.TraverseTo(page.PageLink, pageTypeID);
}

/// <summary>
/// Travers to the nearest parent where specified property has value
/// </summary>
public static PageData TraverseTo(this PageData page, string propertyName)
{
    return GlobalFunctions.TraverseTo(page.PageLink, propertyName);
}

/// <summary>
/// Recurse to the nearest child with the specified PageTypeID
/// </summary>
public static PageData RecurseTo(this PageData page, int pageTypeID)
{
    return GlobalFunctions.RecurseTo(page.PageLink, pageTypeID);
}

/// <summary>
/// Recurse to the nearest child with one of the specified PageTypeID's
/// </summary>
public static PageData RecurseTo(this PageData page, int[] pageTypeID)
{
    return GlobalFunctions.RecurseTo(page.PageLink, pageTypeID);
}

/// <summary>
/// Recurse to the nearest child where specified property has value
/// </summary>
public static PageData RecurseTo(this PageData page, string propertyName)
{
    return GlobalFunctions.RecurseTo(page.PageLink, propertyName);
}

/// <summary>
/// Checks if the specified value exists in the int array
/// </summary>
public static bool Contains(this int[] ints, int value)
{
    foreach (int item in ints)
    {
        if (item == value)
        {
            return true;
        }
    }

    return false;
}
namespace DV
{
    using EPiServer;
    using EPiServer.Core;

    public static class GlobalFunctions
    {
        public static PageData TraverseTo(PageReference from, int pageTypeID)
        {
            return TraverseTo(from, new int[] { pageTypeID });
        }

        public static PageData TraverseTo(PageReference from, int[] pageTypeID)
        {
            if (PageReference.IsNullOrEmpty(from) || from == PageReference.RootPage)
            {
                return null;
            }

            PageData page = DataFactory.Instance.GetPage(from);

            if (pageTypeID.Contains(page.PageTypeID))
            {
                return page;
            }
            else
            {
                return TraverseTo(page.ParentLink, pageTypeID);
            }
        }

        public static PageData TraverseTo(PageReference from, string propertyName)
        {
            if (PageReference.IsNullOrEmpty(from) || from == PageReference.RootPage)
            {
                return null;
            }

            PageData page = DataFactory.Instance.GetPage(from);

            if (IsValue(page, propertyName))
            {
                return page;
            }
            else
            {
                return TraverseTo(page.ParentLink, propertyName);
            }
        }

        public static PageData RecurseTo(PageReference from, int pageTypeID)
        {
            return RecurseTo(from, new int[] { pageTypeID });
        }

        public static PageData RecurseTo(PageReference from, int[] pageTypeID)
        {
            PageData page = null;

            if (!PageReference.IsNullOrEmpty(from))
            {
                foreach (PageData childPage in DataFactory.Instance.GetChildren(from))
                {
                    if (pageTypeID.Contains(childPage.PageTypeID))
                    {
                        return childPage;
                    }
                    else
                    {
                        page = RecurseTo(childPage.PageLink, pageTypeID);
                    }
                }
            }

            return page;
        }

        public static PageData RecurseTo(PageReference from, string propertyName)
        {
            PageData page = null;

            if (!PageReference.IsNullOrEmpty(from))
            {
                foreach (PageData childPage in DataFactory.Instance.GetChildren(from))
                {
                    if (IsValue(childPage, propertyName))
                    {
                        return childPage;
                    }
                    else
                    {
                        page = RecurseTo(childPage.PageLink, propertyName);
                    }
                }
            }

            return page;
        }
    }
}