Convert a LinkItemCollection to a list of PageData

When using the Link collection property in EPiServer CMS I often find my self needing to convert it’s values, that is a collection of LinkItems, into a list of PageData objects. Of course we can’t trust that all of the items are really links to local pages on the site, unless we use some custom property, so I must first check that each item can be mapped to a local page before trying to add it to the list. Luckily, some time ago I wrote a simple utility function for doing just that. Or, more likely, I picked it up on the EPiServer World forum and customized it a bit :)

Anyhow, I’ve been meaning to post this little utility function for quite some time now, and now I’ve finally gotten around to it.

The method is an extension method for the LinkItemCollection class and returns an ordered list of all items in the collection that can be mapped to local pages “converted” to PageData objects. I’ve removed the namespace for brevity.

using System.Collections.Generic;
using EPiServer;
using EPiServer.Core;
using EPiServer.SpecializedProperties;
using EPiServer.Web;

public static class LinkItemCollectionUtility
{
    public static List<PageData> ToPages(this LinkItemCollection linkItemCollection)
    {
        List<PageData> pages = new List<PageData>();

        foreach (LinkItem linkItem in linkItemCollection)
        {
            string linkUrl;
            if (!PermanentLinkMapStore.TryToMapped(linkItem.Href, out linkUrl))
                continue;

            if (string.IsNullOrEmpty(linkUrl))
                continue;

            PageReference pageReference = PageReference.ParseUrl(linkUrl);

            if (PageReference.IsNullOrEmpty(pageReference))
                continue;

            pages.Add(DataFactory.Instance.GetPage((pageReference)));
        }

        return pages;
    }
}

PS. For updates about new posts, sites I find useful and the occasional rant you can follow me on Twitter. You are also most welcome to subscribe to the RSS-feed.

Comments

  1. Frederik Vig's avatar

    Frederik Vig 2 years ago

    I did something similar, but to a PageDataCollection: http://www.frederikvig.com/2009/06/episerver-extension-methods-part-2/.

  2. Ted Nyberg's avatar

    Ted Nyberg 2 years ago

    Great snippet! I like both approaches (both yours and Frederik's), but since I pretty much have a crush on generic lists I'm leaning towards yours... :) Although, as Frederik points out, having a PageDataCollection object helps in using some of the EPiServer API's methods since they accept the PageDataCollection type as the parameter.

  3. Peter's avatar

    Peter 2 years ago

    Thanks! Exactly what I was looking for :)

  4. Daniel Saidi's avatar

    Daniel Saidi 1 years ago

    Great post! Just what we were looking for.

Follow me on Twitter

  1. @chraas You might want to use it for complex parts of the site and not use it for simple rendering pages. 2 days ago
  2. @chraas In theory more SOLID, but I'm not sure it's worth the price. 2 days ago
  3. @chraas Be warned that you're introducing one big chunk of complexity with EPiMVP :) 2 days ago
follow me

Latest comments

  1. Berra S wrote "Read your post at http://joelabrahamsson.com/entry/using-xfo..." on PageData objects not returned as typed when using Page Type Builder and FindPagesWithCriteria
  2. Linus wrote "1 up for behaviour being as close as expected as possible!" on A common problem with Page Type Builder and UniqueValuePerLanguage set to false
  3. Joel Abrahamsson wrote "Hi Hans, Could it be that you previously didn't have Page..." on Page Type Builder 2.0 released

About this site

This blog is built with EPiServer Community, EPiServer CMS, ASP.NET MVC and a bunch of other great products. The source code is available for download at the projects page, where you also can read more about this site and my other projects.

read more