November Happy Hour will be moved to Thursday December 5th.
November Happy Hour will be moved to Thursday December 5th.
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.
When I try to do it I get:
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.
@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.
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.
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));
}
}
}
Thanks, glad you got the idea.
Suppose I missed something when I needed to clear client specific code for the example.
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.
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.
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.