开发者

Umbraco DataTypes. Retrieve list of possible data types.

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 preval开发者_StackOverflowues that are in this drop down list?


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


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;
        }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜