SaaS CMS has officially launched! Learn more now.

Christian Thorvik
Nov 5, 2021
  1175
(1 votes)

Job for deleting removed properties

I've been annoyed by properties staying on page types after deleting them from code, especially on the .Net 5 version where "From Code" doesn't seem to work. I couldn't find a way to do this, so I wrote this quick and dirty job for removing them. I simply load all properties from optimizely, and check if a property with the same name exists on the type, and if not, delete it.

It has not been tested thoroughly, and I would definitely not use it in a production environment, but it saves me a lot of annoynance when working locallly.

This deletes all data related to the property, so use with caution.

public class RemovedDeletedPropertiesJob : ScheduledJobBase
    {
        private readonly IContentTypeRepository _contentTypeRepository;
        private readonly IPropertyDefinitionRepository _propertyDefinitionRepository;

        private int _amountDeleted;
        
        public RemovedDeletedPropertiesJob(IContentTypeRepository contentTypeRepository, 
            IPropertyDefinitionRepository propertyDefinitionRepository)
        {
            _contentTypeRepository = contentTypeRepository;
            _propertyDefinitionRepository = propertyDefinitionRepository;
        }
        
        public override string Execute()
        {
            _amountDeleted = 0;
            
            foreach (var contentType in _contentTypeRepository.List())
            {
                var clone = contentType.CreateWritableClone() as ContentType;
                
                if(clone == null) continue;
                
                foreach (var prop in _propertyDefinitionRepository.List(contentType.ID))
                {
                    try
                    {
                        if (contentType.ModelType.GetProperty(prop.Name) == null)
                        {
                            _propertyDefinitionRepository.Delete(prop);
                            _amountDeleted++;
                        }
                    }
                    catch (Exception e) { }
                }
            }

            return $"Removed {_amountDeleted} properties";
        }
Nov 05, 2021

Comments

Per Nergård
Per Nergård Nov 5, 2021 12:52 PM

I did this MissingProperties initmodule/plugin a long tme ago but the logic in that for identifying missing properties does not work in :Net Core version?

 https://world.optimizely.com/blogs/Per-Nergard/Dates/2016/4/missing-properties-initializationmodule/

Christian Thorvik
Christian Thorvik Nov 5, 2021 01:21 PM

I only found the old blog post where the code is missing, so I haven't seen this one. It seems to work though. Only the appsettings part doesn't work, but that's simple enough to fix

Per Nergård
Per Nergård Nov 5, 2021 01:41 PM

NIce!


Josh Salwen
Josh Salwen Mar 15, 2022 11:51 PM

This works perfectly!

Please login to comment.
Latest blogs
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

How to have a link plugin with extra link id attribute in TinyMce

Introduce Optimizely CMS Editing is using TinyMce for editing rich-text content. We need to use this control a lot in CMS site for kind of WYSWYG...

Binh Nguyen Thi | Jul 13, 2024

Create your first demo site with Optimizely SaaS/Visual Builder

Hello everyone, We are very excited about the launch of our SaaS CMS and the new Visual Builder that comes with it. Since it is the first time you'...

Patrick Lam | Jul 11, 2024

Integrate a CMP workflow step with CMS

As you might know Optimizely has an integration where you can create and edit pages in the CMS directly from the CMP. One of the benefits of this i...

Marcus Hoffmann | Jul 10, 2024