Don't miss out Virtual Happy Hour this Friday (April 26).

Try our conversational search powered by Generative AI!

Praful Jangid
Feb 27, 2019
  2221
(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
Solving the mystery of high memory usage

Sometimes, my work is easy, the problem could be resolved with one look (when I’m lucky enough to look at where it needs to be looked, just like th...

Quan Mai | Apr 22, 2024 | Syndicated blog

Search & Navigation reporting improvements

From version 16.1.0 there are some updates on the statistics pages: Add pagination to search phrase list Allows choosing a custom date range to get...

Phong | Apr 22, 2024

Optimizely and the never-ending story of the missing globe!

I've worked with Optimizely CMS for 14 years, and there are two things I'm obsessed with: Link validation and the globe that keeps disappearing on...

Tomas Hensrud Gulla | Apr 18, 2024 | Syndicated blog

Visitor Groups Usage Report For Optimizely CMS 12

This add-on offers detailed information on how visitor groups are used and how effective they are within Optimizely CMS. Editors can monitor and...

Adnan Zameer | Apr 18, 2024 | Syndicated blog

Azure AI Language – Abstractive Summarisation in Optimizely CMS

In this article, I show how the abstraction summarisation feature provided by the Azure AI Language platform, can be used within Optimizely CMS to...

Anil Patel | Apr 18, 2024 | Syndicated blog

Fix your Search & Navigation (Find) indexing job, please

Once upon a time, a colleague asked me to look into a customer database with weird spikes in database log usage. (You might start to wonder why I a...

Quan Mai | Apr 17, 2024 | Syndicated blog