SaaS CMS has officially launched! Learn more now.

Per Nergård
Feb 19, 2019
  2216
(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
Optimizely release SaaS CMS

Discover the future of content management with Optimizely SaaS CMS. Enjoy seamless updates, reduced costs, and enhanced flexibility for developers...

Andy Blyth | Jul 17, 2024 | Syndicated blog

A day in the life of an Optimizely Developer - London Meetup 2024

Hello and welcome to another instalment of A Day In The Life Of An Optimizely Developer. Last night (11th July 2024) I was excited to have attended...

Graham Carr | Jul 16, 2024

Creating Custom Actors for Optimizely Forms

Optimizely Forms is a powerful tool for creating web forms for various purposes such as registrations, job applications, surveys, etc. By default,...

Nahid | Jul 16, 2024

Optimizely SaaS CMS Concepts and Terminologies

Whether you're a new user of Optimizely CMS or a veteran who have been through the evolution of it, the SaaS CMS is bringing some new concepts and...

Patrick Lam | Jul 15, 2024