Lee Crowe
Feb 5, 2011
(2 votes)

PageTypeBuilder and PropertySettings Synching

Whilst creating my FilteredPageReference properties I had a thought about PageTypeBuilder and PropertySettings.

Currently PageTypeBuilder does not provide a way to synch custom property settings.

For me personally I believe having this ability may be useful as we have the ability to synch page types and property definitions so as the property settings are associated with the property definitions I decided to have a look at what could be added to PageTypeBuilder v1.3 to support this.

Using David Knipe’s post as a reference for programattically setting custom property settings I got to work Smile

The Solution

Ideally I would have liked to set the custom properties via the PageTypePropertyAttribute class like the following:

   1:  [PageTypeProperty(
   2:              EditCaption = "Filtered page reference",
   3:              Searchable = false,
   4:              Type = typeof(PropertyFilteredPageReference),
   5:              Tab = typeof(ContentTab),
   6:              SortOrder = 120,
   7:              PropertySettings = new PropertyFilteredPageReferenceSettings { StartFromCurrentPage = true, AllowQuickEdit = true, PopupHeight = 100 })]
   8:          public virtual PageReference FilteredPageReference { get; set; }


Unfortunately the above causes the following build exception “An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter”.

After giving this a little bit of thought I came up with the following solution.  You will see it is not ideal as some reflection is used but it works!

The Implementation

1. Firstly I modified the PageTypePropertyAttribute.cs class and added the following new properties:

   1:  public virtual string PropertySettingsCreateNewMethodName { get; set; }
   3:  public virtual bool PropertySettingsOverwriteIfChanged { get; set; }
   5:  public virtual bool PropertySettingsOverwriteIfChangedAndGlobal { get; set; }

Properties are described below:

  • PropertySettingsCreateNewMethodName – The name of a method within the PageType class which will return an object which implements IPropertySettings.
  • PropertySettingsOverwriteIfChanged – If true and property settings of the relevant type already exist they will be overwrote, otherwise they will be left unaltered.
  • PropertySettingsOverwriteIfChangedAndGlobal – If true and settings already exist and are global they will be overwrite, otherwise they will be left unaltered.

2. In the PageTypePropertyUpdater.cs class I modified the signature of UpdatePageDefintion to the following:

   1:  protected internal virtual void UpdatePageDefinition(PageDefinition pageDefinition, PageTypePropertyDefinition pageTypePropertyDefinition, PageTypeDefinition pageTypeDefinition)

3. In the PageTypePropertyUpdater.cs class I modified the signature of UpdatePageDefinitionValues to the following:

   1:  protected internal virtual void UpdatePageDefinitionValues(PageDefinition pageDefinition, PageTypePropertyDefinition pageTypePropertyDefinition,
   2:              PageTypeDefinition pageTypeDefinition)

4. Within the UpdatePageDefinitionValues method I added a call to the following call:

   1:  UpdatePropertySettings(pageDefinition, propertyAttribute, pageTypeDefinition);

5. Lastly I added the following new method to PageTypePropertyUpdater.cs which is responsible for updating property settings:

   1:  private void UpdatePropertySettings(PageDefinition pageDefinition, PageTypePropertyAttribute propertyAttribute,
   2:      PageTypeDefinition pageTypeDefinition)
   3:  {
   4:      if (propertyAttribute == null || string.IsNullOrEmpty(propertyAttribute.PropertySettingsCreateNewMethodName))
   5:          return;
   7:      Type typedPageDataType = pageTypeDefinition.Type;
   8:      object typedPageDataInstance = Activator.CreateInstance(typedPageDataType);
   9:      MethodInfo createPropertySettingsMethod = typedPageDataType.GetMethod(propertyAttribute.PropertySettingsCreateNewMethodName, 
  10:          BindingFlags.Instance | BindingFlags.Public);
  12:      if (createPropertySettingsMethod == null)
  13:          return;
  15:      object propertySettings = createPropertySettingsMethod.Invoke(typedPageDataInstance, new object[] { });
  17:      if (propertySettings == null || !(propertySettings is IPropertySettings))
  18:          return;
  20:      IPropertySettingsRepository propertySettingsRepository = new PropertySettingsRepository();
  21:      PropertySettingsContainer container;
  23:      if (pageDefinition.SettingsID == Guid.Empty)
  24:      {
  25:          pageDefinition.SettingsID = Guid.NewGuid();
  26:          pageDefinition.Save(true);
  27:          container = new PropertySettingsContainer(pageDefinition.SettingsID);
  28:      }
  29:      else if (!propertySettingsRepository.TryGetContainer(pageDefinition.SettingsID, out container))
  30:          return;
  32:      bool isGlobal = false;
  33:      bool propertySettingsExist = true;
  35:      PropertySettingsWrapper wrapper = container.Settings.Values
  36:          .Where(current => current.PropertySettings.GetType().Equals(propertySettings.GetType())).FirstOrDefault();
  38:      if (wrapper == null)
  39:      {
  40:          wrapper = new PropertySettingsWrapper();
  41:          propertySettingsExist = false;
  42:      }
  43:      else
  44:          isGlobal = wrapper.IsGlobal;
  46:      if ((propertySettingsExist && !propertyAttribute.PropertySettingsOverwriteIfChanged) ||
  47:          (propertySettingsExist && isGlobal && !propertyAttribute.PropertySettingsOverwriteIfChangedAndGlobal))
  48:      {
  49:          return;
  50:      }
  52:      wrapper.IsGlobal = false;
  53:      wrapper.PropertySettings = propertySettings as IPropertySettings;
  55:      if (!propertySettingsExist)
  56:          container.Settings.Add(propertySettings.GetType().FullName, wrapper);
  58:      propertySettingsRepository.Save(container);
  59:  }

The new method above will firstly check whether a create new method has been specified and if so it will try to get it and invoke it.  The code will then create the relevant settings containers and wrappers if they need to be created and then create or overwrite settings depending on the property attribute values.

An example of setting the relevant attribute values and creating a method to create the custom property settings is below:

   1:  [PageTypeProperty(
   2:      EditCaption = "Filtered page reference",
   3:      Searchable = false,
   4:      Type = typeof(PropertyFilteredPageReference),
   5:      Tab = typeof(ContentTab),
   6:      SortOrder = 120,
   7:      PropertySettingsCreateNewMethodName = "CreateSettings",
   8:      PropertySettingsOverwriteIfChanged = true,
   9:      PropertySettingsOverwriteIfChangedAndGlobal = true)]
  10:  public virtual PageReference FilteredPageReference { get; set; }
  12:  public PropertyFilteredPageReferenceSettings CreateSettings()
  13:  {
  14:      return new PropertyFilteredPageReferenceSettings { StartFromCurrentPage = true, AllowQuickEdit = true, PopupHeight = 100 };
  15:  }


Although not the cleanest solution this may be a useful way of synching page type properties and property settings on different environments!

What do people think?

Joel do you think this is something that could be useful for v2.0 of PageTypeBuilder?

Have feedback?

Please feel free to email or twitter me with any feedback @croweman

Feb 05, 2011


Joel Abrahamsson
Joel Abrahamsson Feb 6, 2011 06:32 PM

Hey Lee!
Thanks for a good post and an interesting solution.
I definitely plan to have support for property settings in the next version. Most likely the implementation will be like I described in this post: http://joelabrahamsson.com/entry/ideas-for-new-features-in-page-type-builder-20, however I will definitely look through this post a number of times when I implement it.

Please login to comment.
Latest blogs
Optimizely SendGrid SMTP host is deprecated

SendGrid is a services for sending email that is included in Optimizely DXP. Previously smtp.episerver.net was the recommended SMTP server to use,...

Tomas Hensrud Gulla | Dec 4, 2022 | Syndicated blog

Hosting Optimizely CMS 12 on Docker Engine

Since Optimizely CMS can now be deployed as a Docker container, here is a demonstration of building, running and scaling an Optimizely CMS 12 site ...

Stefan Holm Olsen | Dec 4, 2022 | Syndicated blog

How to use CacheTagHelper with content areas in Optimizely CMS 12

I might be going out on a limb here - if you have a better solution, feel very free to share it!  Upgrading your Optimizely web application from .N...

Andreas J | Dec 2, 2022

The 1001st Piece in your 1000 Piece Puzzle: .NET Default Interface Functions

I was recently working with a client who wanted a reasonably large subsystem added to Optimizely that would add automated management to their...

Greg J | Nov 28, 2022 | Syndicated blog

Video Demonstration, creating a CMS12 Alloy Sample Site

Hey All Below you will find a quick video demonstration on how to install a local version of Alloy Sample based on CMS12 / .Net 6. As you will see ...

Minesh Shah (Netcel) | Nov 28, 2022

How to create an admin user I Optimizely CMS – with Episerver CLI

In this blog post I’ll show how to create an admin user for Optimizely CMS in a new environment where you don’t have access to the admin interface.

Ove Lartelius | Nov 28, 2022 | Syndicated blog