Skip to content

Instantly share code, notes, and snippets.

@PNergard
Last active April 7, 2016 09:55
Show Gist options
  • Save PNergard/23dfac9c12ce5636a9bddc6bc5c19bca to your computer and use it in GitHub Desktop.
Save PNergard/23dfac9c12ce5636a9bddc6bc5c19bca to your computer and use it in GitHub Desktop.
An initialization module that checks both page- and blocktypes for properties that are no longer defined in code and deletes them. Link to blog post: http://world.episerver.com/blogs/Per-Nergard/Dates/2016/4/missing-properties-initializationmodule/
using System;
using EPiServer.Framework;
using EPiServer.Framework.Initialization;
using System.Web.Configuration;
using EPiServer.DataAbstraction;
using EPiServer.ServiceLocation;
using System.Collections.Generic;
namespace AlloyDemoKit.Business.Initialization
{
[InitializableModule]
[ModuleDependency(typeof(EPiServer.Web.InitializationModule))]
public class MissingProperties : IInitializableModule
{
public void Initialize(InitializationEngine context)
{
if (GoAHead)
{
DeleteMissingProperties(GetMissingProperties());
}
}
public void Uninitialize(InitializationEngine context)
{
//We don't attach any events so we don't do anything
}
/// <summary>
/// Implement whatever activation mechanism that fits your needs.
/// </summary>
private bool GoAHead
{
get
{
return Convert.ToBoolean(WebConfigurationManager.AppSettings.Get("AutoDeleteMissingProperties"));
}
}
/// <summary>
/// Get all properties that are not defined in code anymore.
/// </summary>
/// <returns></returns>
private List<PropertyDefinition> GetMissingProperties()
{
IContentTypeRepository contentTypeRepository = ServiceLocator.Current.GetInstance<IContentTypeRepository>();
List<PropertyDefinition> propertyDefinitions = new List<PropertyDefinition>();
foreach (var type in contentTypeRepository.List())
{
foreach (var property in type.PropertyDefinitions)
{
if (IsMissingModelProperty(property)) propertyDefinitions.Add(property);
}
}
return propertyDefinitions;
}
/// <summary>
/// Is propertydefinition not defind in code?
/// </summary>
/// <param name="propertyDefinition"></param>
/// <returns></returns>
private bool IsMissingModelProperty(PropertyDefinition propertyDefinition)
{
ContentTypeModelRepository typeModelRepository = ServiceLocator.Current.GetInstance<ContentTypeModelRepository>();
return (((propertyDefinition != null) && propertyDefinition.ExistsOnModel) && (typeModelRepository.GetPropertyModel(propertyDefinition.ContentTypeID, propertyDefinition) == null));
}
/// <summary>
/// Delete property definitions
/// </summary>
/// <param name="propertiesToDelete"></param>
private void DeleteMissingProperties(List<PropertyDefinition> propertiesToDelete)
{
IPropertyDefinitionRepository propertyDefinitionRepository = ServiceLocator.Current.GetInstance<IPropertyDefinitionRepository>();
foreach (var propertyDefinition in propertiesToDelete)
{
propertyDefinitionRepository.Delete(propertyDefinition);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment