Try our conversational search powered by Generative AI!

Per Nergård
Feb 24, 2019
  1293
(3 votes)

Automatically create pages for ContentReference properties

If you like me have tried to take an existing solution but wanted to start from scratch, maybe for a demo and you just gave up due to the amount of required properties on for example a settings page? Most of them are probably strings and ints that should be pretty easy to fix (or really should have had good defaults). But what about those pesky properties of type ContentReference which excpects a page of a certain type? And I bet those new pages have more pesky required properties as well! Gaah my blood pressure!

So to come closer to my (pipe?) dream of super easy site site setup even from scratch and using a "mature" solution that has developed over some years I decided to go with an attribute and initmodule approach.

Ok so the attribute is very simple it just takes one parameter with the type we want to get automatically created. So in my example here I have three page types: A startpage that needs a reference to a settingspage which have two references to a searchpage and a newsarchivepage.

So my page type looks like this (RequiredDependec is my custom attribute)

[RequiredDependency(typeof(Settings))]
[AllowedTypes(typeof(Settings))]
public virtual ContentReference SettingsPage {get; set;}

The settingspage looks like this:

[RequiredDependency(typeof(NewsArchive))]
public virtual ContentReference NewsArchive { get; set; }

[RequiredDependency(typeof(ContactCards))]
public virtual ContentReference ContactCards { get; set; }

To fix the automagic creating and setting or properties I did a initmodule that on the onpublish event scans the underlying type with reflection for properties decorated with the new attribute, creates a new page of that type and sets the property to that reference. The created pages are not automatically published since I name them "Automatically created - <pagetypename>" so I guess atleast the name should be changed before they are published. The new pages are created below the ContentReference.StartPage so the startpage need to be created and a site and start page needs to be configured in admin mode.

Ok so what does it look like:

Here we have a unpublished startpage

Here we have the automatically created settingspage

After publishing the settingspage we have a newsarchive and searchpage created

The code is what it is and you use it at your own risk but you can find a complete project over at my Pnergard GitHub

Feb 24, 2019

Comments

Please login to comment.
Latest blogs
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

Azure AI Language – Abstractive Summarisation in Optimizely CMS

In this article, I show how the abstraction summarisation feature provided by the Azure AI Language platform, can be used within Optimizely CMS to...

Anil Patel | Apr 18, 2024 | Syndicated blog

Fix your Search & Navigation (Find) indexing job, please

Once upon a time, a colleague asked me to look into a customer database with weird spikes in database log usage. (You might start to wonder why I a...

Quan Mai | Apr 17, 2024 | Syndicated blog

The A/A Test: What You Need to Know

Sure, we all know what an A/B test can do. But what is an A/A test? How is it different? With an A/B test, we know that we can take a webpage (our...

Lindsey Rogers | Apr 15, 2024

.Net Core Timezone ID's Windows vs Linux

Hey all, First post here and I would like to talk about Timezone ID's and How Windows and Linux systems use different IDs. We currently run a .NET...

sheider | Apr 15, 2024