Copied to clipboard

Flag this post as spam?

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


  • Adriano Fabri 458 posts 1601 karma points
    Oct 14, 2019 @ 13:23
    Adriano Fabri
    0

    GetPrevalueAsString alternative in Umbraco v8

    Hi, I'm trying to get PreValue string by Id.

    With Umbraco v7 I used this code:

    var helper = new UmbracoHelper(UmbracoContext.Current);
    var docCategory = Request["cat"];
    var docCategoryString = helper.GetPreValueAsString(int.Parse(docCategory));
    

    With Umbraco v8 is no longer possible to use this code.

    Can you help me to make my code compliant with umbraco v8?

    Thank you

  • Mike Chambers 635 posts 1252 karma points c-trib
    Oct 14, 2019 @ 15:56
    Mike Chambers
    0

    PreValues are gone in u8 :-(

    https://github.com/umbraco/Umbraco-CMS/issues/4888

    So you should find that the value stored against the field is just the value and not a key to look up the value.

    https://our.umbraco.com/forum/umbraco-8/96369-v8-prevalues-from-a-specific-data-type#comment-313113

  • Adriano Fabri 458 posts 1601 karma points
    Oct 15, 2019 @ 07:04
    Adriano Fabri
    0

    Yes, I know that PreValue are gone in u8, but this is my scenario.

    I receive a param (cat) that is the "Id" of the category that I must use to filter search.

    If I look the field values of the nodes (in the Examine Internal Index), I see

    docCategory:    ["Type 1"]
    

    where "docCategory" is an Umbraco.CheckBoxList

    So...I'm trying to find an easy way to get the name of category using the Id received as a param

  • Mike Chambers 635 posts 1252 karma points c-trib
    Oct 15, 2019 @ 08:13
    Mike Chambers
    0

    But in u8 there is no Id stored? So not sure where you are getting the Id from in your cat param... as there is no Id as such?

    Model.GetPropertyValue("docCategory") will give you "Type 1" not a prevalueId as in Umb7...

  • Adriano Fabri 458 posts 1601 karma points
    Oct 15, 2019 @ 11:11
    Adriano Fabri
    0

    Ok...I solved. I don't know if this is the right way but it function properly.

    I've done a custom extension method to get DataType KVP The result is a Dictionary

    using System.Collections.Generic;
    using System.Linq;
    using Umbraco.Core.Models;
    using Umbraco.Core.PropertyEditors;
    using Umbraco.Web.Composing;
    
    namespace AFStarterKit.AFBusinessLogic
    {
        /// <summary>
        /// Umbraco DataType Extension
        /// </summary>
        public static class DataTypeExtension
        {
            /// <summary>
            /// Get KVP By DataType Name
            /// </summary>
            /// <param name="dataTypeName"></param>
            /// <param name="keyId">0</param>
            /// <returns>List of KVP</returns>
            public static Dictionary<int, string> GetKVPByDataTypeName(string dataTypeName, int keyId = 0)
            {
                return DataTypeListContent(Current.Services.DataTypeService.GetDataType(dataTypeName), keyId);
            }
    
            /// <summary>
            /// Get KVP By DataType Id
            /// </summary>
            /// <param name="dataTypeId"></param>
            /// <param name="keyId">0</param>
            /// <returns>List of KVP</returns>
            public static Dictionary<int, string> GetKVPByDataTypeId(int dataTypeId, int keyId = 0)
            {
                return DataTypeListContent(Current.Services.DataTypeService.GetDataType(dataTypeId), keyId);
            }
    
            /// <summary>
            /// DataType List Content
            /// </summary>
            /// <param name="dataTypeList"></param>
            /// <param name="keyId"></param>
            /// <returns>List of KVP</returns>
            private static Dictionary<int, string> DataTypeListContent(IDataType dataTypeList, int keyId)
            {
                Dictionary<int, string> dataTypeValues = new Dictionary<int, string>();
    
                if (dataTypeList != null)
                {
                    var kvpList = (ValueListConfiguration)dataTypeList.Configuration;
    
                    if (kvpList != null && kvpList.Items != null)
                    {
                        foreach (var kvp in kvpList.Items)
                        {
                            dataTypeValues.Add(kvp.Id, kvp.Value);
                        }
                    }
    
                    if (keyId > 0)
                    {
                        return dataTypeValues.Where(kvp => kvp.Key == keyId).ToDictionary(p => p.Key, p => p.Value);
                    }
    
                    return dataTypeValues;
                }
    
                return null;
            }
        }
    }
    

    So...I now can retrieve the KVP filtered by category/ies

    // DataType Name
    string dataTypeName = "ChkbList-Categories";
    
    // Request param
    int keyId = (!string.IsNullOrEmpty(Request["cat"])) ? int.Parse(Request["cat"].ToString()) : 0;
    
    // Get KVP By DataType Name (filtered)
    var dataTypeKVP = AFStarterKit.AFBusinessLogic.DataTypeExtension.GetKVPByDataTypeName(dataTypeName, keyId);
    

    Now I will work to simplify the code (if possible)...but in the meantime, I hope this can help those with the same need

  • Mike Chambers 635 posts 1252 karma points c-trib
    Oct 15, 2019 @ 11:35
    Mike Chambers
    1

    I was looking at a similar thing, but then discovered that the id's exposed there aren't unique, and are only used by the datatypeeditor. (just a sequential 1, 2, 3 for prevalues added... delete the second and add another and you end up with ids 1,3,4)

    I guess they are unique to a datatype so you can use like you have it.. there is also an associated database cost, as these lookups will hit the DB everytime, cacheing is an option...

    Though I still don't quite get where you are getting param["cat"] from as an id.. how are you retrieving and storing an id.. when as far as umbraco is concerned you should have a checkbox with value="type 1" id="type 1" ???

    Also even though you might have those ids in the datatype config.. if you edit a value on the datatype, any nodes that have the old value stored won't reflect the update as it stores the value and not the id..

    eg if Type 1 needed to now be Option 1, you'd have to change the datatype and then reselect "option 1" on all the nodes that were "type 1" as the param would be unset as "Type 1" no longer exists, even though both would be the same id from the datatype config :-(

    Hope this helps.

    Just had another thought... you could go examine and add a computed field to store the id.. then no db lookups?

    https://our.umbraco.com/documentation/reference/searching/examine/examine-events

  • Adriano Fabri 458 posts 1601 karma points
    Oct 15, 2019 @ 13:07
    Adriano Fabri
    0

    First of all, I want to thank you for giving me support about u8

    I know that it is not clear because you don't know all the contest.

    I try to explain it quickly (sorry my not perfect english) :-)

    In the website we found a Data Type for categories named "dtCategories" (of type Umbraco.CheckboxList) which have these values:

    • Type 1
    • Type 2
    • Type 3
    • etc...

    We found also a Document Type ("doc") that have a property called "docCat" of type "dtCategories" (each "doc" can be associated to one or more categories)

    The template have a column with the list of categories that links to a page that will show a doc list filtered by category id

    So I need a list of links like this:

    <a href="/repository/?cat=1" class="list-group-item list-group-item-action active">Type 1</a>
    <a href="/repository/?cat=2" class="list-group-item list-group-item-action active">Type 2</a>
    etc...
    

    ...where the value of "cat" param must be the Id of category.

    To retrieve the Id of each category, after many attempts, I created a custom method to get all categories from data type name.

    This method return a Dictionary(int, string) that I can use for the list of categories and also to filter docs by category (using the keyId value).

    As I said...I don't think this is the best solution but for now it solves the problem ;-)

  • Mike Chambers 635 posts 1252 karma points c-trib
    Oct 15, 2019 @ 15:15
    Mike Chambers
    0

    no worries..

    I think what u8 expects now would be...

    <a href="/repository/?cat=type 1" class="list-group-item list-group-item-action active">Type 1</a>
    <a href="/repository/?cat=type 2" class="list-group-item list-group-item-action active">Type 2</a>
    

    as that key/value pairing is gone from the IPublishedContent value respresentation of radiobtn/checklist/dropdown

  • Adriano Fabri 458 posts 1601 karma points
    Oct 16, 2019 @ 08:44
    Adriano Fabri
    0

    No...because the name of the categories could be very long and very variables and this could create problems when I have to manage the "cat" parameter

    So, I think that this is actually the best solution

    However...I am looking for a more performing alternative...so...every suggestion is welcome ;-)

Please Sign in or register to post replies

Write your reply to:

Draft