Reputation: 549
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.
Upvotes: 4
Views: 7325
Reputation: 394
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>
}
}
Upvotes: 0
Reputation: 8475
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;
}
Upvotes: 3
Reputation: 176
@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.
Upvotes: 1
Reputation: 3536
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>
}
Upvotes: 5
Reputation: 444
You can use the DataTypeService on the Umbraco helper
Umbraco.DataTypeService.GetPreValuesByDataTypeId()
Upvotes: 4