EPiServer Extension Methods part 2

Posted on June 16, 2009 by Frederik Vig in EPiServer

EPiServer Extension Methods part 1

From LinkItemCollection to PageDataCollection

public static PageDataCollection ToPageDataCollection(this LinkItemCollection linkItemCollection)
{
    var pageDataCollection = new PageDataCollection();
    foreach (var linkItem in linkItemCollection)
    {
	var url = new UrlBuilder(linkItem.Href);
	bool isEPiServerPage = PermanentLinkMapStore.ToMapped(url);
 
	if (isEPiServerPage)
	{
	    var page = DataFactory.Instance.GetPage(PermanentLinkUtility.GetPageReference(url));
 
	    if (page != null)
	    {
		pageDataCollection.Add(page);
	    }
	}
    }
 
    return pageDataCollection;
}

Example

if (IsValue("RelatedPages"))
{
    var relatedLinkItems = (LinkItemCollection) CurrentPage["RelatedPages"];
    if (relatedLinkItems != null)
    {
         var relatedPages = relatedLinkItems.ToPageDataCollection();
         if (relatedPages != null)
         {
             rptRelatedContent = relatedPages;
             rptRelatedContent.DataBind();
         }
    }
}

This is great since you now have the ability to filter out the pages that the user does not have access to or that are not published.

FilterForVisitor.Filter(relatedPages);

Related Posts: