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 110 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 20 posts 132 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 384 posts 489 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.

  • Mike Chambers 614 posts 1191 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

Please Sign in or register to post replies

Write your reply to:

Draft