Try our conversational search powered by Generative AI!

Creating dynamic properties from code


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?

Jun 13, 2013 16:09

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).

Jun 13, 2013 18:46

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?

Jun 17, 2013 10:13

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.

Jun 17, 2013 10:50

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()
        .FirstOrDefault(pd =>
            pd.Name.Equals(name, StringComparison.OrdinalIgnoreCase));

    if (existing != null)
        property.ID = existing.ID;

Feb 20, 2015 1:33
This thread is locked and should be used for reference only. Please use the Episerver CMS 7 and earlier versions forum to open new discussions.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.