Question

I have a property in umbraco that uses a drop down data type with a set of prevalues that you can select from.

How do I retrieve a list of all the possible prevalues that are in this drop down list?

Was it helpful?

Solution

There's a helper method in umbraco.library that does that.

From xslt:

<xsl:variable name="prevalues" select="umbraco.library:GetPreValues(1234)" />

From code:

using umbraco;
XPathNodeIterator prevalues = library.GetPrevalues(1234);

Replace 1234 with the id of your datatype (You can see it in the bottom of your browser when hovering your mouse over the datatype in the developers section)

Regards
Jesper Hauge

OTHER TIPS

Here is the code that I use in one of my Umbraco datatypes to get a DropDownList containing all possible prevalues:

var prevalues = PreValues.GetPreValues(dataTypeDefinitionId);
DropDownList ddl = new DropDownList();

if (prevalues.Count > 0)
{
    for (int i = 0; i < prevalues.Count; i++)
    {
        var prevalue = (PreValue)prevalues[i];
        if (!String.IsNullOrEmpty(prevalue.Value))
        {
            ddl.Items.Add(new ListItem(prevalue.Value, prevalue.DataTypeId.ToString()));
        }
    }
}

Replace dataTypeDefinitionId with the id of your datatype.

I know this is an old question, but I created this method based on the information provided in this answer and I think it is worth documenting:

public static class UmbracoExtensions
{
    public static IEnumerable<string> GetDropDownDataTypeValues(int dataTypeId)
    {
        var dataTypeValues = umbraco.library.GetPreValues(dataTypeId);
        var dataTypeValuesEnumerator = dataTypeValues.GetEnumerator();
        while (dataTypeValues.MoveNext())
        {
            dynamic dataTypeItem = dataTypeValues.Current;
            yield return dataTypeItem.Value;
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top