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
Preview multiple Visitor Groups directly while browsing your Optimizely site

Visitor groups are great - it's an easy way to add personalization towards market segments to your site. But it does come with it's own set of...

Allan Thraen | Sep 26, 2022 | Syndicated blog

The Report Center is finally back in Optimizely CMS 12

With Episerver.CMS.UI 12.12.0 the Report Center is finally re-introduced in the core product.

Tomas Hensrud Gulla | Sep 26, 2022 | Syndicated blog

Dynamic Route in ASP.NET Core When MapDynamicControllerRoute Does Not Work

Background Creating one of the add-on for Optimizely I had to deal with challenge to register dynamically route for the API controller. Dynamic rou...

valdis | Sep 25, 2022 | Syndicated blog

404 Error on Static Assets Within an Optimizely plugin

Background With the move to CMS 12 and .NET 5/6, developers are now able to build Plugins and Extensions using Razor Class Libraries (RCL).  These...

Mark Stott | Sep 23, 2022

How to bypass the content creation view in Optimizely

Something that has come up a couple of times in the last few year is feedback from content editors about the editing view that comes up when creati...

Ynze | Sep 23, 2022 | Syndicated blog

Welcome to Optimizely World's New Tech Video Portal

Optimizely, leader in the digital experience realm, has become a wealth of world class SaaS products including Web Experimentation, Full Stack, B2B...

The Developer Marketing Team of Optimizely | Sep 22, 2022