November Happy Hour will be moved to Thursday December 5th.

Praful Jangid
Feb 27, 2019
  2344
(9 votes)

Sorting dictionaries / translations with Episerver events

What we are trying to achieve here is, whenever I create a new entry in Translations Block (to create dictionary item), the latest entry goes at the end of the list. Which is not sorted in alphabetic order. Therefore, it is difficult to find any entry as the list size increases.

So, we have a TranslationsBlock, that contains Translations property list type. This Translations contains three properties (Key, Phrase and Description).

We will apply sorting in alphabetic ascending (a->z) order. First on Key, then Phrase and then Description.

 

Here we go,

Create an Initialization module named EventInitialization (whatever name you prefer) and decorate it with attributes

[InitializableModule]
[ModuleDependency(typeof(EPiServer.Web.InitializationModule))] 

Also, don’t forget to inherit class with interface IInitializableModule.

public class EventInitialization : IInitializableModule 

Implement the methods of this interface.

Now, inside the Initialize (implemented from interface) method bind your event.

var events = ServiceLocator.Current.GetInstance<IContentEvents>();
events.PublishedContent += this.OnSavedContent;

I attached it to PublishContent event, that will be raised as the content is being published.

Now, the finishing work goes inside the OnSavedContent method.

private void OnSavedContent(object sender, ContentEventArgs contentEventArgs)
{
    if (contentEventArgs.Content is TranslationsBlock translationsBlock)
    {
        var repository = ServiceLocator.Current.GetInstance<IContentRepository>();
        var block = (TranslationsBlock)translationsBlock.CreateWritableClone();
        block.Translations = block.Translations?
            .OrderBy(x => x.Key)
            .ThenBy(x => x.Phrase)
            .ThenBy(x => x.Description)
            .ToList();
        repository.Save((IContent)block, SaveAction.Publish);
    }
}

Here, first I checked for the content type is our required type TranslationsBlock or not. Then creating a writable clone to update it. Now apply your operations and save it with the help of Save method of ContentRepository.

Happy Coding :)

Thanks,

Praful Jangid

Feb 27, 2019

Comments

Please login to comment.
Latest blogs
Optimizely SaaS CMS + Coveo Search Page

Short on time but need a listing feature with filters, pagination, and sorting? Create a fully functional Coveo-powered search page driven by data...

Damian Smutek | Nov 21, 2024 | Syndicated blog

Optimizely SaaS CMS DAM Picker (Interim)

Simplify your Optimizely SaaS CMS workflow with the Interim DAM Picker Chrome extension. Seamlessly integrate your DAM system, streamlining asset...

Andy Blyth | Nov 21, 2024 | Syndicated blog

Optimizely CMS Roadmap

Explore Optimizely CMS's latest roadmap, packed with developer-focused updates. From SaaS speed to Visual Builder enhancements, developer tooling...

Andy Blyth | Nov 21, 2024 | Syndicated blog

Set Default Culture in Optimizely CMS 12

Take control over culture-specific operations like date and time formatting.

Tomas Hensrud Gulla | Nov 15, 2024 | Syndicated blog