Copied to clipboard

Flag this post as spam?

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


  • Eugen 30 posts 131 karma points
    Dec 06, 2018 @ 15:45
    Eugen
    0

    Clearing the cache with a code

    Hello. I am changing the value of some properties of the DropDownList editor in the database from C# code. My way works and everything is fine. Only here the property values remain the same until I restart the entire project. I understand that before the project is reloaded, it takes the value from the cache. How can I do it with a code so that after certain actions the cache is cleared?

  • Frans de Jong 548 posts 1840 karma points MVP 3x c-trib
    Dec 06, 2018 @ 16:03
    Frans de Jong
    0

    Are you changing the properies on a contentnode? Can you show us a code example of what you are doing?

  • Kevin Jump 2310 posts 14695 karma points MVP 7x c-trib
    Dec 06, 2018 @ 17:16
    Kevin Jump
    100

    Hi Eugen,

    I really would caution against going directly to the DB and bypassing Umbraco's services - I know it's tempting because it is quicker, but caching is just one of the issues you will hit going this way.

    Instead if you want to update a dropdown list, better to use the DataTypeService and alter the prevalues that way.

    var item = _dataTypeService.GetDataTypeDefinitionByName("mydropdownlist");
    var existingPreValues = _dataTypeService.GetPreValuesCollectionByDataTypeId(item.Id)
                                   .FormatAsDictionary();
    
    // existingPreValues is a list of alias, and value, so you can update it as needed
    
    // then save them back
    _dataTypeService.SavePreValues(item, existingPreValues);
    

    this will update the values in the db, and the cache.

    Kevin

  • Eugen 30 posts 131 karma points
    Dec 07, 2018 @ 08:07
    Eugen
    0

    Thank you so much Kevin for the tip. I want to try this way. But I am making a plugin and in my decision the name of editor will not be known in advance. Is there a function where I can take all the names and execute your decision for each editor?

  • Eugen 30 posts 131 karma points
    Dec 07, 2018 @ 08:14
    Eugen
    0

    Looks like I found. I used in my decision

     var _dataTypeService = ApplicationContext.Current.Services.DataTypeService;
                        var editor = _dataTypeService.GetDataTypeDefinitionById(property.PropertyType.DataTypeDefinitionId);
                        var existingPreValues = _dataTypeService.GetPreValuesCollectionByDataTypeId(editor.Id)
                                                       .FormatAsDictionary();
    

    Thanks again Kevin!

  • Eugen 30 posts 131 karma points
    Dec 07, 2018 @ 08:30
    Eugen
    0

    Kevin sorry again. I have implemented this solution, it works, only the problem remains. Until I restart the project, not all Prevalue are updated. What else should I try to do?

  • Eugen 30 posts 131 karma points
    Dec 07, 2018 @ 08:36
    Eugen
    0

    And another problem is that this solution only works with those PreValues of checkbox that are checked. Those PreValues that are but not selected, for some reason, it ignores :(

  • Kevin Jump 2310 posts 14695 karma points MVP 7x c-trib
    Dec 07, 2018 @ 09:18
    Kevin Jump
    1

    Ah OK, slightly crossed wires here.

    From the last comment i think you are trying to change these values on a content item - except they are not actually stored against the content item.

    So dropdown values (the text you see) are stored in the datatype, and you pick them from the content - the content stores the id of the item in the list (but it shows you the text on screen), so you can't actually change this on the content without changing it for everyone in the datatype (so all versions of the content using the drop down have their values changed)

    I am assuming this is related to your language stuff of other posts ? if so then the answer for lists is not to actually translate them but use the umbraco dictionary to change the values.

    One way i would look to achieve this on a site is to have dictionary item values that match the names in the dropdowns, and translate them on the page

    so for example if we have a dropdown/checkbox list with the following

    • MyFirstValue
    • MySecondValue
    • MyThirdValue

    i would create dictionary items

    MyList.MyFirstValue, MyList.MySecondValue MyList.MyThirdvalue

    then in the site code something like (this might be wrong syntax- just typing of the top of my head here)

    var listvalue = Model.GetPropertyValue<string>("myListItem");
    <div>@Umbraco.GetDictionaryItem("MyList." + listValue)</div>
    

    Then the page will render the right value for the list from the dictionary, which means you haven't had to change a site wide value or mess about with hidden things.

    You might be able to do this with dictionary values in the dropdown - for example if you call the value #MyList.MySecondValue then umbraco will show the translated text in the back office and return it - but to be honest i have not tried this with dropdown lists so i am not sure if it works like that.

Please Sign in or register to post replies

Write your reply to:

Draft