Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Ólafur Hólm Eyþórsson 3 posts 94 karma points
    Mar 18, 2019 @ 18:04
    Ólafur Hólm Eyþórsson
    1

    V8 Prevalues from a specific data type

    Hi, how do i get the prevalues from a specific data type in v8 ?

    at my work place we've always been doing the example below in v7

    public static string GetPreValue(string id)
    {
       var value = string.Empty;
       if (!string.IsNullOrEmpty(id))
       {
           int preId = 0;
           var parse = Int32.TryParse(id, out preId);
    
           if (parse)
           {
               var helper = new UmbracoHelper(UmbracoContext.Current);
    
               value = helper.GetPreValueAsString(preId);
           }
       }
       return value;
    }
    

    and

    public List<SelectListItem> GetPreValuesFromDataTypeId(int dataTypeId)
            {
                List<SelectListItem> preValueSelectorList = new List<SelectListItem>();
    
                XPathNodeIterator iterator = umbraco.library.GetPreValues(dataTypeId);
                iterator.MoveNext();
                XPathNodeIterator preValues = iterator.Current.SelectChildren("preValue", "");
    
                while (preValues.MoveNext())
                {
                    string preValueIdAsString = preValues.Current.GetAttribute("id", "");
                    int preValueId = 0;
                    int.TryParse(preValueIdAsString, out preValueId);
                    string preValue = preValues.Current.Value;
                    preValueSelectorList.Add(new SelectListItem { Value = preValue, Text = preValue });
                }
    
                return preValueSelectorList;
            }
    

    but that is no longer possible to do :/

    So any information on this would be of appreciated :)

  • David Knittl 9 posts 109 karma points
    Mar 20, 2019 @ 12:10
    David Knittl
    0

    Hi,

    I have a similar problem. I also work with the prevalues of a dropdown field in my code. In v7 I accessed the prevalues through

    Services.DataTypeService.GetPreValuesCollectionByDataTypeId(dataTypeId).PreValuesAsDictionary
    

    That's not working any more in v8. Now through debugging I found a property called "Configuration" on the IDataType interface. All prevalues are in there (I see them in debugging) and that object is of type

    Umbraco.Core.PropertyEditors.DropDownFlexibleConfiguration
    

    Now if I try to access this property and cast it to DropDownFlexibleConfiguration (or if I use the "ConfigurationAs

    Does anyone have an idea how to access the prevalues in v8?

    Cheers, David

  • Philip Hayton 19 posts 131 karma points
    Apr 27, 2019 @ 09:52
    Philip Hayton
    0

    Did anybody get to the bottom of this?

  • Rhys Hamilton 140 posts 933 karma points
    May 22, 2019 @ 09:18
    Rhys Hamilton
    0

    I'm not sure if this is the best way to do this, but a sample class that I've put together, returns the ID for a prevalue that matches a string value (e.g - the id for "Item1" in my dropdown list). It's a bit specific to my use case, but it retrieves the prevalues perfectly for me.

    private readonly IDataTypeService _dataTypeService;
    
    public DefaultComposer(IDataTypeService dataTypeService)
    {
        _dataTypeService = dataTypeService;
    }
    
    // Additional methods/logic can go here
    // etc. etc.
    
    // Returns the ID of the prevalue that matches the string value
    private int GetDataTypePreValues(int id, string value)
    {
        var dataType = _dataTypeService.GetDataType(id); // The ID of the DataType you want to access
        ValueListConfiguration prevalues = (ValueListConfiguration)dataType.Configuration;
        int selected = 0;
    
        if (prevalues != null && prevalues.Items != null && prevalues.Items.Any())
        {
            selected = prevalues.Items.Where(x => x.Value == value).Select(i => i.Id).FirstOrDefault();
        }
    
        return selected;
    }
    
  • andrew shearer 379 posts 484 karma points
    1 day ago
    andrew shearer
    0

    hello - is there an equivalent helper method of GetPreValueAsString in v8 yet? maybe on second thoughts we shouldn't be using it anyway as it results in a sql hit to get the prevalue.. from the cache would be better.

Please Sign in or register to post replies

Write your reply to:

Draft