Praful Jangid
Feb 27, 2019
  2263
(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
Opti ID overview

Opti ID allows you to log in once and switch between Optimizely products using Okta, Entra ID, or a local account. You can also manage all your use...

K Khan | Jul 26, 2024

Getting Started with Optimizely SaaS using Next.js Starter App - Extend a component - Part 3

This is the final part of our Optimizely SaaS CMS proof-of-concept (POC) blog series. In this post, we'll dive into extending a component within th...

Raghavendra Murthy | Jul 23, 2024 | Syndicated blog

Optimizely Graph – Faceting with Geta Categories

Overview As Optimizely Graph (and Content Cloud SaaS) makes its global debut, it is known that there are going to be some bugs and quirks. One of t...

Eric Markson | Jul 22, 2024 | Syndicated blog

Integration Bynder (DAM) with Optimizely

Bynder is a comprehensive digital asset management (DAM) platform that enables businesses to efficiently manage, store, organize, and share their...

Sanjay Kumar | Jul 22, 2024