World is now on Opti ID! Learn more

Mikael Nordberg
May 11, 2009
  3201
(0 votes)

Strongly typed property access- Part two

As promised by Daniel in part one, here is part two!

In this post I’ll try to explain how we could automatically deploy our mapped properties that we use to get strongly typed properties (see Daniels post) in our templates by using some reflection, conventions and attributes.

Creating a new Template

First we create a new Web Form/Page Template in Visual Studio and change the inheritance from EPiServer.TemplatePage to our own extended class TemplatePage<T>. 

To add properties we create a class-file for the properties that we will use in our template. Neither the PageType or associated properties has to be present in EPiServer.

   1: public class MyPageProperties : PropertiesBase
   2: {        
   3:     public PropertyString Heading { get; set; }
   4:     public PropertyXhtmlString MainBody { get; set; }        
   5:     public PropertyUrl SomeUrl { get; set; }
   6: }

PageTypeBuilder

PageTypeBuilder is the tool responsible for creating and updating  PageTypes and properties in EPiServer.

To execute it, we need to add some code when the application starts, for instance in a PlugIn or in Global.asax.

   1: public class PageTypeDefPlugin : EPiServer.PlugIn.PlugInAttribute
   2: {
   3:     public static void Start()
   4:     {
   5:         var builder = new PageTypeBuilder<PropertiesBase>(Assembly.GetExecutingAssembly().FullName)
   6:                           {
   7:                                   Settings = new PageTypeBuilderSettings {TabName = "Information", VirtualPathRoot = @"\Pages\"}
   8:                           };
   9:  
  10:         builder.UpdatePageTypes();
  11:     }
  12: }

PropertiesBase is added as a generic type and the BuildPageTypes method takes a params string[] with names of assemblies to scan for mapping classes.

The tool will look for classes that are subclasses of T (= PropertiesBase) and build PageTypes out of these. Each class will become a PageType and each property will be added as a  EPiServer property.

For global default settings a PageTypeBuilderSettings object is used. Here it’s possible to add virtual path, default tab, if unmapped properties should be deleted  or not and if existing properties should be updated.

Attributes

But what about Help texts,  Tabs and Descriptions? The tool will guess/use conventions for these. The class name - “Properties” will be the name for the PageType (class “StartProperties” = PageType “Start”), if no Tab name is specified, the property will be added to the “Information” tab (if not overridden by the global settings) and so forth.

To override this and to add additional information we could decorate our mapping classes with attributes.  Attributes could be Name of the page/property, Edit Heading, Tab, Default value, Language specific and some others. Pretty much the same as we have in Admin mode.

If we have properties that are shared between templates these could be added to either the PropertiesBase class or to another class that inherits PropertiesBase and then use this class as subclass for our mappings.  Since we don’t want this class to be a PageType there is a [Ignore] attribute that could come in handy.

The sort order of the properties will be the same as the order in our mapping classes.

Screenshots

some pictures might say more  than 1000 words…

 1.pagetypes
Before

For mapping class we use this:

   1: public class MyPageProperties : PropertiesBase
   2: {
   3:     [HelpText("Ingress")]        
   4:     public PropertyString MainIntro { get; set; }
   5:     [HelpText("Primär text")]        
   6:     public PropertyXhtmlString MainBody { get; set; }
   7:     [HelpText("Länk till bild")]        
   8:     public PropertyImageUrl ImageUrl { get; set; }
   9: }

This will result in this new PageType:  
2.mypage   Sort order of properties will be the same as in the mapping class

Code is king!

Since we add, update and delete properties when the application starts we will not depend on a specific database. This could be useful in a continuous integration scenario where the development database differs from the database used for test and of course if all developers in a team have their own (local) database.

Summary

This tool is pretty much a “proof of concept” that could be used to complement our way to handle strongly typed properties. It' can not (yet) handle all features of PageTypes that exists in EPiServer (dynamic properties to mention one) but will ease the pain of creating PageTypes and  adding properties ;)

So, what do you think?

May 11, 2009

Comments

Please login to comment.
Latest blogs
Make Global Assets Site- and Language-Aware at Indexing Time

I had a support case the other day with a question around search on global assets on a multisite. This is the result of that investigation. This co...

dada | Jun 26, 2025

The remote server returned an error: (400) Bad Request – when configuring Azure Storage for an older Optimizely CMS site

How to fix a strange issue that occurred when I moved editor-uploaded files for some old Optimizely CMS 11 solutions to Azure Storage.

Tomas Hensrud Gulla | Jun 26, 2025 |

Enable Opal AI for your Optimizely products

Learn how to enable Opal AI, and meet your infinite workforce.

Tomas Hensrud Gulla | Jun 25, 2025 |

Deploying to Optimizely Frontend Hosting: A Practical Guide

Optimizely Frontend Hosting is a cloud-based solution for deploying headless frontend applications - currently supporting only Next.js projects. It...

Szymon Uryga | Jun 25, 2025

World on Opti ID

We're excited to announce that world.optimizely.com is now integrated with Opti ID! What does this mean for you? New Users:  You can now log in wit...

Patrick Lam | Jun 22, 2025

Avoid Scandinavian Letters in File Names in Optimizely CMS

Discover how Scandinavian letters in file names can break media in Optimizely CMS—and learn a simple code fix to automatically sanitize uploads for...

Henning Sjørbotten | Jun 19, 2025 |