Get typed pagedata by page guid in EPiServer CMS
January 3rd, 2012In EpiServer you might sometimes need to get PageData, preferably typed, from a page guid.
Here is a simple method for getting a page only by sending a guid as an argument, which we can do thanks to the PermanentPageLinkMap class, and results of other pagetypes than the requested are ignored, as well as any permanent links which are file links.
1 2 3 4 5 6 7 8 9 10 11 12 13 | public static T GetPage<T>(Guid pageGuid) where T : PageData { var linkMap = EPiServer.Web.PermanentLinkMapStore.Find(pageGuid) as PermanentPageLinkMap; if (linkMap != null) { var page = DataFactory.Instance.GetPage(linkMap.PageReference); if (page != null && page is T) { return (T)page; } } return null; } |
Tested in EPiServer CMS 6, but should work fine in version 5 as well.