EPiServer Extension Methods part 2
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);
Excellent code snippet, that’s very useful. LinkItemCollection is neat to use.