How to add PreValues programmatically into DropdownList in v8?
Hi folks,
I found some examples for v7 and earlier, but not for v8.
Anyone knows how to do this?
I have so far:
public static void SetDataTypePreValues(int id, List<string> values)
{
var dataTypeService = Current.Services.DataTypeService;
var dataType = dataTypeService.GetDataType(id); // this is the dropdownlist
// and now???
}
var valueList = (ValueListConfiguration)dataType.Configuration;
var lastId = valueList.Items.LastOrDefault()?.Id;
var newValueList = new List<ValueListConfiguration.ValueListItem>();
var idCounter = lastId ?? 0;
foreach (var val in values)
{
newValueList.Add(new ValueListConfiguration.ValueListItem
{
Id = idCounter,
Value = val
});
idCounter++;
}
((ValueListConfiguration)dataType.Configuration).Items.AddRange(newValueList);
dataTypeService.Save(dataType);
I don't know if this is the best way to do it, but at least it works.
How to add PreValues programmatically into DropdownList in v8?
Hi folks,
I found some examples for v7 and earlier, but not for v8. Anyone knows how to do this?
I have so far:
Thank you for your help.
Peter
What I did for now is:
I don't know if this is the best way to do it, but at least it works.
Thanks for sharing Peter!
By combining your value adding with this answer: https://our.umbraco.com/forum/extending-umbraco-and-using-the-api/97597-creating-custom-data-type#comment-308044
I was able to create a composer to migrate a collection of select lists from a bespoke CMS. Hopefully this helps someone else.
Works like a charm, I wouldn't know how to include ValueListConfiguration property. Thanks buddy
is working on a reply...