I'm writing a user control (web forms) that creates a dropdownlist -- the dropdown list needs to get its keys/values from a dropdown list data type that I've created in Umbraco. The Umbraco dropdown data type is using integer.
I'm able to get the strings for the dropdown by :
var categoryType = service.GetAllDataTypeDefinitions().FirstOrDefault(x=>x.Name== "Visitor Category"); var prevalues = service.GetPreValuesByDataTypeId(categoryType.Id);
But, I'm not sure how to get the integer value for each prevalue, since "GetPreValuesByDataTypeId" only returns an array of strings.
Sorry, I think you misunderstood my question (or I didn't state it clearly):
If I have a dropdown list with prevalues as follows: Text="Teacher", Value=11 Text="Student", Value=12 ... and so on...
The call to GetPreValuesByDataTypeId only returns a string array with ["Teacher", "Student"]... not the corresponding integer values. If I'm constructing that dropdown list in code (from my own user control), I need both the text and value.
Oh bugger, what a silly unprepared answer I gave you. Sorry! :)
I'm out of time and memory at the moment, but here's a few pointers:
If you set the prevalue editor of your datatype to be a KeyValuePrevalueEditor, you'll get access to both values.
The property will have the ID of the selected value(s) when you use it.
Here's a bit of code:
public class SomeDataType : BaseDataType, IDataType
{
//...
public override IDataEditor DataEditor
{
get
{
if (editor == null)
editor = new SomeEditor(Data, ((KeyValuePrevalueEditor)PrevalueEditor).PrevaluesAsKeyValuePairList);
return editor;
}
}
//...
public override IDataPrevalue PrevalueEditor
{
get
{
if (prevalueeditor == null)
prevalueeditor = new KeyValuePrevalueEditor(this);
return prevalueeditor;
}
}
//...
}
public class SomeEditor : DropDownList, IDataEditor
{
//...
public SomeEditor(IData data, List<KeyValuePair<int, string>> prevalues)
{
this.data = data;
this.prevalues = prevalues;
}
//...
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
if (prevalues != null)
{
foreach (KeyValuePair<int, string> keyValuePair in prevalues)
Items.Add(new ListItem(keyValuePair.Value, keyValuePair.Key.ToString()));
}
//...
}
}
A bit late to the party here, but this took a long time to dig up from the internet... This is for version 7.1.6
I am attempting to import document type(s) from an Umbraco package (package.xml). CheckBoxLists (and probably other types) provide selected items as text, but the cmsPropertyData table requires the IDs.
Looking at only Umbraco.CheckBoxLists at the moment, I used the following back end C# code to obtain a "," delimited string of selected ids.
The preValues you get, are an array of keys and values, where the value contains both the Id and the Value.
var contentService = ApplicationContext.Current.Services.ContentService;
var dataTypeService = ApplicationContext.Current.Services.DataTypeService;
if (genericProperty.Type.InvariantEquals("Umbraco.CheckBoxList"))
{
// obtain the data type definition (we need the nodeId, as that too is not in the xml)
var dataTypeDefinition = dataTypeService.GetDataTypeDefinitionById(new Guid(genericProperty.Definition).Value));
// get pre values (ids / values) - [0, Umbraco.CoreModels.PreValue] where PreValue: (Key, Value) - Value.Id, Value.SortOrder, Value.Value
var preValues = dataTypeService.GetPreValuesCollectionByDataTypeId(dataTypeDefinition.Id).PreValuesAsDictionary;
// get "," delimited string of ids where .Value is in stringValue
var value = String.Join(",", preValues.Where(v => stringValue.Contains(v.Value.Value)).Select(i => i.Value.Id).ToArray());
}
genericProperty - I obtained from Umbraco's package.xml export, for example:
Getting integer values from prevalue list
I'm writing a user control (web forms) that creates a dropdownlist -- the dropdown list needs to get its keys/values from a dropdown list data type that I've created in Umbraco. The Umbraco dropdown data type is using integer.
I'm able to get the strings for the dropdown by :
var categoryType = service.GetAllDataTypeDefinitions().FirstOrDefault(x=>x.Name== "Visitor Category");
var prevalues = service.GetPreValuesByDataTypeId(categoryType.Id);
But, I'm not sure how to get the integer value for each prevalue, since "GetPreValuesByDataTypeId" only returns an array of strings.
I'm using 6.0.5.
Thanks
All of that stuff is stored as text in the database, and as far as I know it's up to you handle the "serialization".
You can easily convert it to integers yourself:
Lars-Erik
Sorry, I think you misunderstood my question (or I didn't state it clearly):
If I have a dropdown list with prevalues as follows:
Text="Teacher", Value=11
Text="Student", Value=12 ... and so on...
The call to GetPreValuesByDataTypeId only returns a string array with ["Teacher", "Student"]... not the corresponding integer values. If I'm constructing that dropdown list in code (from my own user control), I need both the text and value.
Thanks for your response.
Oh bugger, what a silly unprepared answer I gave you. Sorry! :)
I'm out of time and memory at the moment, but here's a few pointers:
If you set the prevalue editor of your datatype to be a
KeyValuePrevalueEditor
, you'll get access to both values. The property will have the ID of the selected value(s) when you use it.Here's a bit of code:
A bit late to the party here, but this took a long time to dig up from the internet... This is for version 7.1.6
I am attempting to import document type(s) from an Umbraco package (package.xml). CheckBoxLists (and probably other types) provide selected items as text, but the cmsPropertyData table requires the IDs.
Looking at only Umbraco.CheckBoxLists at the moment, I used the following back end C# code to obtain a "," delimited string of selected ids.
The preValues you get, are an array of keys and values, where the value contains both the Id and the Value.
genericProperty - I obtained from Umbraco's package.xml export, for example:
stringValue - the text(s) for the id(s) I need, for example:
Jo
is working on a reply...