Search code examples
c#.netumbracoumbraco7

Umbraco 7 - Update dictionary item's value via code


There's Umbraco's Dictionary section which contains all its items.
I'm trying to find a way to update the value of a specific item via my code (.NET).
I know how can I get the value, but don't know how to set and update it.
Any recommendations?


Solution

  • If you are using Umbraco's LocalizationService to create your dictionary items and get them programmatically, then this is how you update an existing dictionary item's value;

    // Get the existing dictionary item to delete it
    var dictionaryItem = _localizationService.GetDictionaryItemByKey(data.Key);
        
    // Get all languages
    var allActiveLanguages=_localizationService.GetAllLanguages();
                            
    // Set your language - I only have one language, which is why I'm getting the first one here
    var language = allActiveLanguages.FirstOrDefault();
        
                            
    if (language != null)
    {
     _localizationService.AddOrUpdateDictionaryValue(dictionaryItem, language, "New dictionary value is here!!");
    }
    
    // Save your updates    
    _localizationService.Save(dictionaryItem);
    

    PS: Below image is how you can use the ILocationService.

    PSS: For more code examples, please take a look at here.

    enter image description here

    enter image description here