4

I am using Umbraco 7 and I have created a data type that uses the property type dropdown list publishing keys. How can I get the id of each prevalue?

Thanks in advance.

wingyip
  • 3,465
  • 2
  • 34
  • 52
sidy3d
  • 440
  • 2
  • 8
  • 22

5 Answers5

5

Something ike this.

You need to reference:

@using umbraco.cms.businesslogic.datatype

Then get the Datatype Id from :

var dataTypeId = umbraco.cms.businesslogic.datatype.DataTypeDefinition
                .GetAll().First(d=> d.Text == "DataTypeName").Id;

var preValues = PreValues.GetPreValues(dataTypeId).Values;
var enumerator = preValues.GetEnumerator();
while (enumerator.MoveNext())
{
    var preValueText = ((PreValue)enumerator.Current).Value;
    <option>@preValueText</option>
}
Ocaso Protal
  • 19,362
  • 8
  • 76
  • 83
wingyip
  • 3,465
  • 2
  • 34
  • 52
4

You can use the DataTypeService on the Umbraco helper

Umbraco.DataTypeService.GetPreValuesByDataTypeId()

John Churchley
  • 434
  • 4
  • 17
3

In Umbraco 7.x, umbraco.cms.businesslogic.datatype.DataTypeDefinition is deprecated.

Instead, I used the following. Thanks to @Kerpalito's answer for the start, but I didn't want to have to hard-code my Data Type's ID, as it can change between environments. The name is the same in all environments.

public List<string> GetPrevalues()
{
        List<string> toReturn = new List<string>();

        IDataTypeDefinition dataType = ApplicationContext.Current.Services.DataTypeService.GetDataTypeDefinitionByName("My Data Type Name");

        if (dataType == null)
        {
            return toReturn;
        }

        PreValueCollection preValues = ApplicationContext.Current.Services.DataTypeService.GetPreValuesCollectionByDataTypeId(dataType.Id);

        if (preValues == null)
        {
            return toReturn;
        }

        IDictionary<string, PreValue> tempDictionary = preValues.FormatAsDictionary();

        toReturn = tempDictionary.Select(p => p.Value.Value).ToList();

        return toReturn;
}
ps2goat
  • 8,067
  • 1
  • 35
  • 68
1
   @foreach (var categoryPrevalue in ApplicationContext.Services.DataTypeService.GetPreValuesByDataTypeId(**-42**).ToList())

      {
        <li><a href="#">@categoryPrevalue</a></li>
      }

"-42" should be changed to your Datatypeid in Umbraco backoffice.

Kerpalito
  • 177
  • 1
  • 8
0

In Umbraco 8, use this code:

@{
          var _dataTypeService = Services.DataTypeService;
          var blogCategories = (DropDownFlexibleConfiguration)_dataTypeService.GetDataType(1142).Configuration;
          foreach (var value in blogCategories.Items)
          {
            <option value="@value.Value">@value.Value</option>
          }
        }