I read this thread but the answers did not make any sense.
I wanna make sure that my dynamic properties are created when i either deploy or start out with a fresh database.
So i guess i could check and create missing dynamic properties in an initialization module, but how?
I would strongly suggest you try to avoid using dynamic properties completely if possible.
First of all, editors sometimes have problems understanding the results of dynamice properties and how to use them.Also, it's supposed to be quite performance consuming (at least it was in earlier versions).And on top of that, it seems to me to be buggy - the inheritance checkbox is not considered at all so values are inherited regardless when setting a value to the dynamic property. (At least in version 7.1)
An alternative, depending on your requirements, could be to create a property for all pagetypes which should be able to have a value (preferably using a base class or interface), and then you read the value with a method which takes currentpage as an arguments and tries to get the value, if not trying it's parents value and so on. The value can be read through the global viewmodel (like the LayoutModel class in Alloy).
But what if i do insist on using dynamic properties (actually i think they are awesome when used with care). How do i create them from code?
If you really want to use them you'll need to create an Initializable Module where you in one way or another checks and creates a Dynamic Property.
To create a Dynamic Property you basically need to create a PropertyDefinition and make sure that ContentTypeID is 0 and save it with an implementation of IPropertyDefinitionRepository (ServiceLocator.GetInstance<IPropertyDefinitionRepository>()).
Have a look at the class EditPropertyDefinition in EPiServer.UI if you want to see how EPiServer creates Dynamic Properties.
For future reference, this a helper method you can use:
private void EnsureDynamicProperty(string name, int sortOrder, bool languageSpecific, string tabName, Type type)
var repository = ServiceLocator.Current.GetInstance<IPropertyDefinitionRepository>();
var typeRepository = ServiceLocator.Current.GetInstance<IPropertyDefinitionTypeRepository>();
var tabRepository = ServiceLocator.Current.GetInstance<ITabDefinitionRepository>();
var property = new PropertyDefinition()
ContentTypeID = 0,
DisplayEditUI = true,
EditCaption = name,
FieldOrder = sortOrder,
LanguageSpecific = languageSpecific,
Name = name,
Tab = tabRepository.Load(tabName),
Type = typeRepository.Load(type)
var existing = repository.ListDynamic()
if (existing != null)
property.ID = existing.ID;