Try our conversational search powered by Generative AI!

Praful Jangid
Feb 27, 2019
  2234
(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
From Procrastination to Proficiency: Navigating Your Journey to Web Experimentation Certification

Hey there, Optimizely enthusiasts!   Join me in celebrating a milestone – I'm officially a certified web experimentation expert! It's an exhilarati...

Silvio Pacitto | May 17, 2024

GPT-4o Now Available for Optimizely via the AI-Assistant plugin!

I am excited to announce that GPT-4o is now available for Optimizely users through the Epicweb AI-Assistant integration. This means you can leverag...

Luc Gosso (MVP) | May 17, 2024 | Syndicated blog

The downside of being too fast

Today when I was tracking down some changes, I came across this commit comment Who wrote this? Me, almost 5 years ago. I did have a chuckle in my...

Quan Mai | May 17, 2024 | Syndicated blog

Optimizely Forms: Safeguarding Your Data

With the rise of cyber threats and privacy concerns, safeguarding sensitive information has become a top priority for businesses across all...

K Khan | May 16, 2024