In this tutorial, you will learn how you can automatically configure the order of content pages within the Episerver CMS editor. When a company decides to power their website using Episerver CMS, they usually have a content team that updates the website's content on a frequent basis. Despite best intentions, content editors can become a bit lazy when adding new content and forget to organise their pages within the backend. News and blog pages are usually the worst offenders. Editors will tend to keep on adding more content under the same parent in the editor. Hundreds of pages may exist under one node. Having too many items in the same location will slow down the CMS and make it harder for editors to find things. One quick and very simple way to make the CMS easier to manage is to implement a scheduled task that runs on a regular basis and re-orders the pages in alphabetical order.
As most news items and blogs render on the website by date added, it's no biggie to change the sort order in the CMS by page name. Implementing something to make content editors' lives easier, can be an easy quick win that can keep a marketing department happy for another week 😊 Luckily, implementing a scheduled task to do this is pretty simple. If you want to learn how then read on ️🔥️🔥️🔥
The snippet above can be copied and pasted into your codebase. The page type that I am ordering pages by is ProductPage
. On Line 34, you will obviously need to change contentTypeRepository.Load();
to use a page type within your project. In the code above, I use the ContentTypeRepository
to get a reference to the product page type. On Line 35, using the reference to the product page type, I use the IContentModelUsage
API to get all product page instances within the CMS. After we have this list, it is a simple case of iterating through the list and setting the sort order for each page incrementally and then re-publishing.
This code works and it is simple. There are a few improvements you could make. You check if the existing page had been published, if it hasn't been excluded it from the process. Second, the ordering is done global rather than on a per section basis. If you want to be really anal about the sort order, you could create an array that keeps a sort order for each parent, rather than creating one big counter, however, its more code and the result would be the same. It is up to you if you want to do that. Happy Coding 🤘