Anders Hattestad
Feb 16, 2011
  9558
(2 votes)

Auto create sub page(s) when a page is created

imageThere are times when I want to make a structure of pages when one page is created. Sometimes it would be easy to just add an attributes to your page type, so each time a page of that type is created we will auto magically add a sub page.

 

First I made myself a attribute

Code Snippet
  1. [AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
  2. public class EnsureSubPageAttribute : Attribute
  3. {
  4.     public Type PageType { get; set; }
  5.     public string PageName { get; set; }
  6.     public string URLSegment { get; set; }
  7.     public int SortIndex { get; set; }
  8.     public string SetReferenceToProperty { get; set; }
  9.     public string[] SetPropertyNames { get; set; }
  10.     public string[] SetPropertyValues { get; set; }
  11. }

Then I add myself to the Created Page event. I do this stuff there since created page is only triggered once, and if I change my attributes I will ensure my sub pages on startup.

Code Snippet
  1. [ModuleDependency(typeof(PageTypeBuilder.Initializer))]
  2. public class EnsureSubPages : IInitializableModule
  3. {
  4.     #region IInitializableModule Members
  5.     public void Initialize(EPiServer.Framework.Initialization.InitializationEngine context)
  6.     {
  7.         EPiServer.DataFactory.Instance.CreatedPage += new EPiServer.PageEventHandler(Instance_CreatedPage);
  8.         EnsureAllExistingPages();
  9.     }
  10.        
  11.     public void Preload(string[] parameters) { }
  12.  
  13.     public void Uninitialize(EPiServer.Framework.Initialization.InitializationEngine context)
  14.     {
  15.         EPiServer.DataFactory.Instance.CreatedPage -= new EPiServer.PageEventHandler(Instance_CreatedPage);
  16.     }
  17.     #endregion

The method for created page looks like this

Code Snippet
  1. void Instance_CreatedPage(object sender, EPiServer.PageEventArgs e)
  2. {
  3.     EnsureSubPagesForPage(e.Page);
  4. }
  5. static void EnsureSubPagesForPage(PageData page)
  6. {
  7.     var type = page.GetType();
  8.     PageDataCollection subPages = null;
  9.     foreach (var attribute in type.GetCustomAttributes(true))
  10.     {
  11.         var myAttribute = (attribute as EnsureSubPageAttribute);
  12.         if (myAttribute != null && !string.IsNullOrEmpty(myAttribute.URLSegment))
  13.         {
  14.             if (subPages == null)
  15.                 subPages = EPiServer.DataFactory.Instance.GetChildren(page.PageLink, LanguageSelector.MasterLanguage());
  16.             var result = (from subPage in subPages where subPage.URLSegment == myAttribute.URLSegment select subPage).ToList();
  17.             if (result.Count == 0)
  18.                 page = CreatePage(page, myAttribute);
  19.             else
  20.                 UpdatePage(myAttribute, result[0]);
  21.         }
  22.     }
  23.     if (page.IsModified)
  24.         EPiServer.DataFactory.Instance.Save(page, EPiServer.DataAccess.SaveAction.ForceCurrentVersion | EPiServer.DataAccess.SaveAction.Publish, EPiServer.Security.AccessLevel.NoAccess);
  25. }

but the cool (and dangerous ) task is the method that checks all the page types and find all that uses the attribute.Then finds all pages of that type and ensure them.

Code Snippet
  1. private static void EnsureAllExistingPages()
  2. {
  3.     foreach (var pagetype in EPiServer.DataAbstraction.PageType.List())
  4.     {
  5.         var typedPageType = PageTypeResolver.Instance.GetPageTypeType(pagetype.ID);
  6.         if (typedPageType != null && typedPageType.GetCustomAttributes(typeof(EnsureSubPageAttribute), true).Length > 0)
  7.         {
  8.             var pages = FindPagesByPageType(typedPageType, PageReference.StartPage);
  9.             foreach (var page in pages)
  10.                 EnsureSubPagesForPage(page);
  11.         }
  12.     }
  13. }

 

So now one can in the code behind mark page types with attributes like this

Code Snippet
  1. [PageType(Name = "Department structur", Filename = "/Custom/Pages/Department.aspx", DefaultSortIndex = 10)]
  2. [EnsureSubPage(
  3.     PageType = typeof(AttachPageProviderPageType),
  4.     PageName = "Community pages",
  5.     SetPropertyNames=new string[] {"AttachChildrenFrom"},
  6.     SetPropertyValues=new string[] {"22"},
  7.     SetReferenceToProperty = "OneClubPage",
  8.     URLSegment = "CommunityPages",
  9.     SortIndex = 10)]
  10. [EnsureSubPage(
  11.     PageType = typeof(UDPageType),
  12.     PageName = "dummy page",
  13.     URLSegment = "DummyPage",
  14.     SortIndex=20)]
  15. [EnsureSubPage(
  16.     PageType = typeof(UDPageType),
  17.     PageName = "dummy page 2",
  18.     URLSegment = "DummyPage2",
  19.     SortIndex = 30)]
  20. public class DepartmentPageType : TypedPageData, OnCreatedPage, IClubRoot

Full code is here. Its just one file :)

Feb 16, 2011

Comments

Feb 17, 2011 04:03 PM

Neat solution.
I think I'll have to replace my global event handlers with your code.

Please login to comment.
Latest blogs
Opti ID overview

Opti ID allows you to log in once and switch between Optimizely products using Okta, Entra ID, or a local account. You can also manage all your use...

K Khan | Jul 26, 2024

Getting Started with Optimizely SaaS using Next.js Starter App - Extend a component - Part 3

This is the final part of our Optimizely SaaS CMS proof-of-concept (POC) blog series. In this post, we'll dive into extending a component within th...

Raghavendra Murthy | Jul 23, 2024 | Syndicated blog

Optimizely Graph – Faceting with Geta Categories

Overview As Optimizely Graph (and Content Cloud SaaS) makes its global debut, it is known that there are going to be some bugs and quirks. One of t...

Eric Markson | Jul 22, 2024 | Syndicated blog

Integration Bynder (DAM) with Optimizely

Bynder is a comprehensive digital asset management (DAM) platform that enables businesses to efficiently manage, store, organize, and share their...

Sanjay Kumar | Jul 22, 2024

Frontend Hosting for SaaS CMS Solutions

Introduction Now that CMS SaaS Core has gone into general availability, it is a good time to start discussing where to host the head. SaaS Core is...

Minesh Shah (Netcel) | Jul 20, 2024

Optimizely London Dev Meetup 11th July 2024

On 11th July 2024 in London Niteco and Netcel along with Optimizely ran the London Developer meetup. There was an great agenda of talks that we put...

Scott Reed | Jul 19, 2024