Dynamic property drop down list populated from code.

Vote:
 

I'd like to do this: http://joelabrahamsson.com/limiting-content-and-page-reference-properties-to-values-of-a-specific-type-in-episerver-cms/ to populate my Dynamic Property of type 'Dynamic list (one option)'. 

Basicaly instead of putting values into config file like:

<add key="MyDynamicValueProperty" value="one|two|three" />

I would like any other way to populate the values from code.

#71896
May 31, 2013 13:01
Vote:
 

You essentially just need to populate the values within the ISelectionFactory. Check out Joel's other post regarding populating via an Enum: http://joelabrahamsson.com/enum-properties-with-episerver/, and look at the code for the selection factory. That's where you'll do the dynamic population of the property.

#71926
May 31, 2013 17:16
Vote:
 

When I try to do it I get:

  • Could not find an appSettings key for "Test1".In order to use dynamic property with dynamic list type add property values to <appSettings> section:<add key="Test1" value="text1;value1|text2;value2|text3;value3..." />

 

#71939
Jun 01, 2013 21:45
Vote:
 

It sounds to me that you're trying to use the old Property Type called "Dynamic List (one option)" and comes from the PropertyData "PropertyAppSettings". There is a multiple option version of this as well.

These populates their dropdown fields from the AppSettings section in Web.config where the Key in AppSettings is the name of your current Property.

To make sure that you're doing it correctly, please check following:

How is your Property written in your PageData class?

Normally a Property in EPiServer 7 is added like this. In this example I want to add a string property.

[Display(Name = "Name of my Property that is displayed for the editor",
Description = "Description of my property",
Order = 10)]
public virtual string Test1 { get; set;}

By adding a UIHint attribute you can define how the UI displays the property to the editor such as dropdown list or other control. There are some variations but this is the simplest of them.

Some examples are:

[UIHint("AppSettings")] if you accually want to have a dropdown list populated from Web.config.

[UIHint("DropDownList")] if you want a dropdown list where an editor can add options in the Admin mode.

The blog posts mentioned earlier explains this quite good how to add options for the editor programmatically:

http://joelabrahamsson.com/enum-properties-with-episerver/ and http://joelabrahamsson.com/limiting-content-and-page-reference-properties-to-values-of-a-specific-type-in-episerver-cms/.

Another good blog post is written by Linus Ekström: http://world.episerver.com/Blogs/Linus-Ekstrom/Dates/2012/9/EPiServer-7-Configuring-editors-for-your-properties/.

Is the Property Type correct?

Find your PageType in Admin mode and check your Property Type.

It should be String, Number or whatever kind of property you want. Sometimes you need to "Revert to Default" to update your code changes to the database.

Mostly when I'm working with Properties in code and they don't end up as I want to, I look for clues in the admin mode.

#71944
Edited, Jun 02, 2013 14:53
Vote:
 

@Alf & @Chris Thanks for replies, your solution works work fine for me unless I want to use them a "Dynamic Property" (or I miss something).

I did "MultipleLanguage" exampe from http://world.episerver.com/Blogs/Linus-Ekstrom/Dates/2012/9/EPiServer-7-Configuring-editors-for-your-properties/ and it works like expected but I want now to have one shared property for all MyPageType.

Steps:

Admin mode->'Config' tab->'Dynamic Properties'->'Add Property'

and there I have to specify a 'Type' and this Type will always override what I have set on my property definition (by UIHint as in the examples).

When I choose 'Drop-down list' I can edit the valuse on 'Custom Settings' tab, but I would need it from code.

 

#71986
Jun 04, 2013 12:21
Vote:
 

Sorry I missed that with Dynamic Property in your question.

In that case I would create a CustomProperty that inherits PropertyDropDownList, override CreatePropertyControl method:

public override IPropertyControl CreatePropertyControl()
{
    return new MySpecialPropertyDropDownControl();
}

Create MySpecialPropertyDropDownControl that inherits PropertyDropDownListControl and override these methods:

public class MySpecialPropertyDropDownControl : PropertyDropDownListControl
{
    public DropDownListProperty DropDownListValue
    {
        get { return PropertyData as PropertyDropDownList; }
    }

    protected override void SetupEditControls()
    {
        SetupDropDownList();
    }

    protected virtual void SetupDropDownList()
    {
        AddDropDownListItems();

        if (DropDownListProperty != null && DropDownListProperty.Value != null)
        {
            ListItem li = EditControl.Items.FindByValue(DropDownListProperty.Value as string);
            if (li != null)
                li.Selected = true;
        }
        EditControl.DataBind();
    }

    protected virtual void AddDropDownListItems()
    {
        var myOptions = SomeMethodThatGetMyOptions();
        foreach (var option in myOptions)
        {
            EditControl.Items.Add(new ListItem(option.Text, option.Value));
        }
    }
}

    

It's definitely not a "clean" solution but it's the closest thing I've seen. Also works when adding properties to Dynamic Content.

#71990
Edited, Jun 04, 2013 13:09
Vote:
 
#71991
Jun 04, 2013 14:32
Vote:
 

Thanks Alf! Works great.

Just small remarks:

CustomProperty should have attributes [PropertyDefinitionTypePlugIn] [Serializable].

 

MySpecialPropertyDropDownControl should be like:

public class MySpecialPropertyDropDownControl : PropertyDropDownListControl
{
    public PropertyDropDownList DropDownListValue
    {
        get { return PropertyData as PropertyDropDownList; }
    }

    protected override void SetupEditControls()
    {
        SetupDropDownList();
    }

    protected virtual void SetupDropDownList()
    {
        AddDropDownListItems();

        if (DropDownListValue != null && DropDownListValue.Value != null)
        {
            ListItem li = EditControl.Items.FindByValue(DropDownListValue.Value as string);
            if (li != null)
                li.Selected = true;
        }
        EditControl.DataBind();
    }

    protected virtual void AddDropDownListItems()
    {
        var myOptions = SomeMethodThatGetMyOptions();
        foreach (var option in myOptions)
        {
            EditControl.Items.Add(new ListItem(option.Text, option.Value));
        }
    }
}

    

 

#71992
Jun 04, 2013 14:37
Vote:
 

Thanks, glad you got the idea.

Suppose I missed something when I needed to clear client specific code for the example.

#71993
Jun 04, 2013 14:41
Vote:
 

The sad think is that I have to use CustomProperty (UIHint is not enough). This should be solved, more info here http://world.episerver.com/Modules/Forum/Pages/thread.aspx?id=66081&pageIndex=1.

#72013
Jun 05, 2013 11:03
Vote:
 

If you want to use the UIHint DropDownlist you should remember to apply a backingtype aswell. Like this.


[UIHint("DropDownList")]
[BackingType(typeof(PropertyDropDownList))]
[Display(GroupName = SystemTabNames.Content, Order = 160)]
public virtual string Test1 { get; set; }

Using this approach you have to manuelly define the selectitems from EPiServer admin mode for that property. 

#75908
Oct 10, 2013 17:49
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.