Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the string value of a prevalue in umbraco

Tags:

c#

umbraco

I have added a few prevalues to a dropdown-list. When Im trying to get the value of a property that uses my dropdownlist-data-type I keep getting the "key" of the preValue as the value of the property.. but thats not what Im after.. I need and want to get the actuallt prevalue-string that I entered as my prevalue..

Any ideas?

Oh and btw.. I need to do it using C#..

EDIT

This is what I have tried so far.. and it sort of works.. but It retrives a list of lists that I cant do any quering against..

PreValues.GetPreValues(new Document(statistic.ParentId,true).getProperty("identifier").PropertyType.DataTypeDefinition.DataType.DataTypeDefinitionId).Values
like image 916
Inx Avatar asked Feb 16 '23 20:02

Inx


2 Answers

I am using below approch to get dropdown list value as text and id

// Node ID of content node in which dropdown has been used 
int NodeIdOfContent = 1111; 
Document docMfsFeedSettings = new Document(NodeIdOfContent); 

int preValueID = docMfsFeedSettings.getProperty("dropDownAliasName").Value)

// Retrieve Text value 
string textValue = umbraco.library.GetPreValueAsString(
                  Convert.ToInt32(docMfsFeedSettings.getProperty("dropDownAliasName").Value));    
like image 133
Nishant Kumar Avatar answered Feb 19 '23 09:02

Nishant Kumar


It's kind of a mess, but using the PreValues object here's what you would do to get an IEnumerable of just the values:

...

int id = statistic.ParentId;
var node = new Document(id, true);
var property = node.getProperty("dropdown");
var dataTypeDefinitionId = property.PropertyType.DataTypeDefinition.DataType.DataTypeDefinitionId;

// Cast the SortedList to an IEnumerable of DictionaryEntry and then
// select the PreValue objects from it:
var preValues = PreValues.GetPreValues(dataTypeDefinitionId)
    .Cast<DictionaryEntry>()
    .Select(d => d.Value as PreValue);

// Select just the values from the PreValue objects:
var values = preValues.Select(p => p.Value);

Another approach would be to use the umbraco.library.GetPrevalues(int id) method. However both approaches are messy in their own ways.

like image 28
Douglas Ludlow Avatar answered Feb 19 '23 10:02

Douglas Ludlow