Don't miss out Virtual Happy Hour this Friday (April 26).

Try our conversational search powered by Generative AI!

Anders Hattestad
Feb 16, 2011
  9501
(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
Solving the mystery of high memory usage

Sometimes, my work is easy, the problem could be resolved with one look (when I’m lucky enough to look at where it needs to be looked, just like th...

Quan Mai | Apr 22, 2024 | Syndicated blog

Search & Navigation reporting improvements

From version 16.1.0 there are some updates on the statistics pages: Add pagination to search phrase list Allows choosing a custom date range to get...

Phong | Apr 22, 2024

Optimizely and the never-ending story of the missing globe!

I've worked with Optimizely CMS for 14 years, and there are two things I'm obsessed with: Link validation and the globe that keeps disappearing on...

Tomas Hensrud Gulla | Apr 18, 2024 | Syndicated blog

Visitor Groups Usage Report For Optimizely CMS 12

This add-on offers detailed information on how visitor groups are used and how effective they are within Optimizely CMS. Editors can monitor and...

Adnan Zameer | Apr 18, 2024 | Syndicated blog