Five New Optimizely Certifications are Here! Validate your expertise and advance your career with our latest certification exams. Click here to find out more

Per Nergård (MVP)
Feb 19, 2019
  2440
(2 votes)

Warning before deleting a page that is used in a property decorated with a SelectOne or SelectMany attribute

Long title and I hope it made some sense.

The case is that it's quite common that the underlying data for SelectionFactories which is then used with SelectOne and SelectMany attributes is normal Episerver pages. For example this is used to handle contact-cards in the Alloy site.

But since it's pages there is always a risk that someone by mistake deletes such a page causing problems. We would really like to have Episervers built in is this page used somewhere check.

The fix for a SelectOne attribute is as easy as having the underlying property to be of type ContentReference instead of a string. But if you have a SelectMany the underlying property needs to be a string and the chosen values is just added as a comma separated string.

To fix this for SelectMany I have created a custom attribute and a validation attribute class. When you add a SelectMany attribute where the SelectionFactory returns ContentReferences as id's you also add the new custom attribute which specifies an IList<ContentReference> property. The validation attribute then adds / removes references to the IList property automatically. With this when we try to delete a referenced page we get the builtin reference check functionality.

Pseudo code:

SelectMany(typeof(factoryclass))
SelectManyListProperty("ListPropertyName")
public virtual string SelectManyProp { get; set; }

public virtual IList ListPropertyName {get; set;}

Before

After

Note that the ContentReference property isn't updated until page is published (values are there) and ofcourse it should be hidden under an other tab or atleast disabled for editors.

Code over at my Gist.

Feb 19, 2019

Comments

Please login to comment.
Latest blogs
Extending UrlResolver to Generate Lowercase Links in Optimizely CMS 12

When working with Optimizely CMS 12, URL consistency is crucial for SEO and usability. By default, Optimizely does not enforce lowercase URLs, whic...

Santiago Morla | Mar 7, 2025 |

Optimizing Experiences with Optimizely: Custom Audience Criteria for Mobile Visitors

In today’s mobile-first world, delivering personalized experiences to visitors using mobile devices is crucial for maximizing engagement and...

Nenad Nicevski | Mar 5, 2025 |

Unable to view Optimizely Forms submissions when some values are too long

I discovered a form where the form submissions could not be viewed in the Optimizely UI, only downloaded. Learn how to fix the issue.

Tomas Hensrud Gulla | Mar 4, 2025 |

CMS 12 DXP Migrations - Time Zones

When it comes to migrating a project from CMS 11 and .NET Framework on the DXP to CMS 12 and .NET Core one thing you need to be aware of is the...

Scott Reed | Mar 4, 2025