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 13 posts 114 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 98 posts 435 karma points
    Apr 27, 2019 @ 09:52
    Philip Hayton
    0

    Did anybody get to the bottom of this?

  • Rhys Hamilton 140 posts 942 karma points
    May 22, 2019 @ 09:18
    Rhys Hamilton
    1

    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;
    }
    
  • Peter Aderhold 30 posts 204 karma points
    Jan 05, 2020 @ 13:12
    Peter Aderhold
    0

    This is exactly the way we do it and it works.

  • andrew shearer 513 posts 663 karma points
    Sep 18, 2019 @ 20:16
    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.

  • Rasmus Styrk 1 post 72 karma points
    Sep 24, 2019 @ 04:41
    Rasmus Styrk
    1

    Hi guys,

    I got the prevalues for my dropdown field with this code.

    @using Umbraco.Core.Services;
    @using Umbraco.Core.PropertyEditors;
    
    @{
        IDataTypeService dataTypeService = Services.DataTypeService;
        IDataType dataType = dataTypeService.GetDataType(1154); // Change number to your id from the data types section in backoffice
        ValueListConfiguration prevalues = (ValueListConfiguration)dataType.Configuration;
    
        foreach (ValueListConfiguration.ValueListItem item in prevalues.Items) {
            <p>@item.Value</p>
        }
    }
    
  • Mike Chambers 636 posts 1253 karma points c-trib
    Oct 09, 2019 @ 12:17
    Mike Chambers
    0

    Just stumbled across this myself, but think prevalue id's now aren't unique? as seeing cloud udas..

    "EditorAlias": "Umbraco.CheckBoxList",
      "DatabaseType": 1,
      "Configuration": {"items":[{"id":1,"value":"Head of State"},{"id":2,"value":"Government Minister"},{"id":3,"value":"Civil Servant"},{"id":4,"value":"NGO Worker"},{"id":5,"value":"Teacher"},{"id":6,"value":"Student"},{"id":7,"value":"General Public"}]},
    

    and another checkboxlist on the same site..

      "EditorAlias": "Umbraco.DropDown.Flexible",
      "DatabaseType": 1,
      "Configuration": {"multiple":false,"items":[{"id":2,"value":"Course"},{"id":3,"value":"E-Book"},{"id":4,"value":"Event"},{"id":6,"value":"Network"},{"id":7,"value":"News"},{"id":10,"value":"Video"},{"id":9,"value":"External Link"},{"id":11,"value":"Briefing"},{"id":12,"value":"Report"},{"id":13,"value":"Learning Resource"},{"id":14,"value":"Teaching Resource"},{"id":15,"value":"Infographic"},{"id":16,"value":"Policy Document"},{"id":17,"value":"Webinar"},{"id":18,"value":"Paper"}]},
    

    and values stored in the db are json.. jsut the value.UmbracoPropertyData table

  • Phil Whittaker 63 posts 267 karma points MVP 4x c-trib
    Dec 15, 2019 @ 09:55
    Phil Whittaker
    2

    After lots of hunting around the code, the way that Umbraco 8 does this is

           var propEditor = Current.PropertyEditors.Where(x => x.Alias == dataType.EditorAlias).FirstOrDefault();
           var config = propEditor.GetConfigurationEditor().ToValueEditor(dataType.Configuration);
    

    The first line just gets you the property editor for the DataType, the second line is the important one, it returns an IDictionary

    This can be serialised into a json object

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies