SaaS CMS has officially launched! Learn more now.

Praful Jangid
Feb 27, 2019
  2257
(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 Concepts and Terminologies

Whether you're a new user of Optimizely CMS or a veteran who have been through the evolution of it, the SaaS CMS is bringing some new concepts and...

Patrick Lam | Jul 15, 2024

How to have a link plugin with extra link id attribute in TinyMce

Introduce Optimizely CMS Editing is using TinyMce for editing rich-text content. We need to use this control a lot in CMS site for kind of WYSWYG...

Binh Nguyen Thi | Jul 13, 2024

Create your first demo site with Optimizely SaaS/Visual Builder

Hello everyone, We are very excited about the launch of our SaaS CMS and the new Visual Builder that comes with it. Since it is the first time you'...

Patrick Lam | Jul 11, 2024

Integrate a CMP workflow step with CMS

As you might know Optimizely has an integration where you can create and edit pages in the CMS directly from the CMP. One of the benefits of this i...

Marcus Hoffmann | Jul 10, 2024

GetNextSegment with empty Remaining causing fuzzes

Optimizely CMS offers you to create partial routers. This concept allows you display content differently depending on the routed content in the URL...

David Drouin-Prince | Jul 8, 2024 | Syndicated blog

Product Listing Page - using Graph

Optimizely Graph makes it possible to query your data in an advanced way, by using GraphQL. Querying data, using facets and search phrases, is very...

Jonas Bergqvist | Jul 5, 2024