Try our conversational search powered by Generative AI!

Lee Crowe
Mar 19, 2012
  3258
(0 votes)

PageTypeBuilder v2.0 – Property Groups and Dynamic Properties

Some of you may have read Joel’s post on Working with Dynamic Properties and Page Type Builder but if not you may want to check the article Smile.

The other day somebody asked me whether there was a nice solution to using property groups in a dynamic property kind of way as explained in Joel’s article. 

This got my mind working a bit and although the solution I have knocked together is not particularly nice as it uses quite a bit of reflection it seems to do the job Smile.

The Solution

Step One

Firstly you will need to download the PropertyGroupExtensions.cs class I have knocked together from here and add it to your solution.

The class contains an extension method for classes that inherit TypedPageData.  The method is named GetPropertyGroupDynamicProperty.

 

Step Two

In this example I am going to demonstrate having an Image Property Group, the code definition is below.  You will notice that the class implements IPropertyGroupComparer.  This interface contains an IsNull method which you must implement to determine whether you would class the property group as being null or empty.

  1: public class Image : PageTypePropertyGroup, IPropertyGroupComparer
  2: {
  3: 
  4:     [PageTypeProperty(
  5:         EditCaption = "Image url", 
  6:         Type = typeof(PropertyImageUrl),
  7:         SortOrder = 100)]
  8:     public virtual string ImageUrl { get; set; }
  9: 
 10:     [PageTypeProperty(
 11:         EditCaption = "Alternative text", 
 12:         Type = typeof(PropertyString),
 13:         SortOrder = 110)]
 14:     public virtual string AltText { get; set; }
 15: 
 16:     public string GetHtml()
 17:     {
 18:         return string.IsNullOrEmpty(ImageUrl) ? string.Empty 
 19:             : string.Format("<img src=\"{0}\" alt=\"{1}\" />", ImageUrl, AltText);
 20:     }
 21: 
 22:     public bool IsNull()
 23:     {
 24:         return string.IsNullOrEmpty(ImageUrl) &&
 25:                 string.IsNullOrEmpty(AltText);
 26:     }
 27: }

Step Three

Now we have our property group defined.  I shall add the property to my CommonPageTypeBase class.  This is a class that inherits TypedPageData but also is used s a base class to all page types.  So any properties that are added to this class will be available on all page types.

  1: public class CommonPageTypeBase : TypedPageData
  2: {
  3:     [PageTypePropertyGroup(
  4:         EditCaptionPrefix = "Header image dynamic property - ", 
  5:         StartSortOrderFrom = 150)]
  6:     public virtual Image HeaderImageDynamicProperty
  7:     {
  8:         get
  9:         {
 10:             return this.GetPropertyGroupDynamicProperty<CommonPageTypeBase, Image>
 11:                 (p => p.HeaderImageDynamicProperty);
 12:         }
 13:         set
 14:         {
 15:             // do nothing, this has to be implemented
 16:         }
 17:     }
 18: }

You can see in the class definition above that we have added a property group definition named HeaderImageDynamicProperty

You will also noticed I have implemented the get and set methods. 

The get method implementation calls the GetPropertyGroupDynamicProperty extension method with two Generic type parameters.  The first one is a class that inherits TypedPageData, where as the second is a class that implements PageTypePropertyGroup and IPropertyGroupComparer.

When the get method is called it will basically go up the page hierarchy until it reaches the start page or finds a populated Image property group.

Feedback

As always feedback is greatly appreciated, if you have found any bugs or  have any suggestions of improvement please let me know.

Just twitter me @croweman or send me an email Smile

Mar 19, 2012

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