In this article I’d like to describe new Episerver extension for converting pages in edit mode. It can be useful for Editors to simplify updates of site structure and keeps links to the content.
Converting pages in edit mode

Converting pages in admin mode

In Admin mode, there is a Convert Pages tool that allows to convert pages in tree structure from one page type into another. Administrator sets the source page (or the root page when converting together with subpages), source and target page types and property mappings. Any properties with matching types can be converted. Properties that are set to “Remove property permanently” won’t be available after the conversion.
Admin mode Convert Pages
The tool is flexible and allows to convert any page types. It can be especially useful during upgrading the site when existing content has to be migrated.

Converting pages in edit mode

The Convert Pages tool is not available for Edit Mode. Converting one page type into another has to be a conscious decision, because of the possibility of data loss. But is it always risky?
Below is the structure of page models in Alloy demo site.
Alloy classes structure
Converting between ContactPage and StartPage won’t be very useful for Editors, but Article, News and Product share same base class. For example we could have a StandardPage that should be converted to ArticlePage. Because ArticlePage inherits from StandardPage, the StandardPage properties is a subset of ArticlePage properties and there will be no data loss.
Alloy StandardPage subclasses
What is the benefit of converting pages instead of creating new page instance with content copy? First of all Editor has to create new page and manually copy all properties from one page to another. But also, if the page is referenced by other pages, those references will be lost when deleting old page. Those references have to be added manually.
page references
When converting a page, the ContentLink won’t change and all links will be available after the conversion.

Converting pages extension

When using my extenstion Editor has limited option to convert pages in Edit Mode. There is no bulk conversion, only one page can be converted at time. All available conversions has to be registered using PageConversionSettingsRepository. The repository stores mappings for page types. Every mapping has:

  • Source page type id
  • Target page type id
  • Property mappings – list of KeyValue pairs containing source property id and target property id

For one page type there could be more than one conversion registered. For example, for StandardPage we could have conversions to ArticlePage, NewsPage and ProductPage.

Operating on IDs when configuring page types and properties is a bit inconvenient to use, so I implemented a builder that simplify creating mappings. Builder is a generic class and use property expressions to add property mappings. For example to create a mapping from StandardPage to ArticlePage you can create builder: new PropertyMappingsBuilder<StandardPage,ArticlePage>(contentTypeRepository) and then add properties using AddMapping method. AddMapping has two parameters: expression for source property and expression for target property:

But in most cases (like in example above), for simple edit mode conversions all property names will match. That’s why builder has also “AddAllWithSameNameAnType” method that automatically maps all properties that has same name and type.

Of course when converting from Article to StandardPage properties from Article that can’t be mapped will be lost.

The result of “Build” method should be saved in repository. To register mappings we should use ModuleInitializer. The full example of mapping between StandardPage and base classes:

When fields are not match we can use “AddMapping” method and convert one property into another. For example I have ContactPage with two subclasses EmployeePage and CustomerPage. When converting from Employee to Customer, the “EmployeeId” property from EmployeePage should be copied to “CustomerId” property from CustomerPage. When converting from Customer to Employee, the “CustomerId” from CustomerPage should be copied to “AdditionalInfo” on EmployeePage.

Conversions are located under Page Tools commands. When only one conversion for page is available, like Article can be converted only to News, then single menu option is created.
Converting pages with one conversion
When current page has many conversions, like StandardPage can be converted to Article, News and Product, then submenu is displayed.
Converting pages with many conversions

Running the conversion command will use the same API as the admin mode tool.

Below is a demo of converting StandardPage to ArticlePage:
Using Converting pages in edit mode

The source code together with working example is available on Github (edit-mode-page-convert branch).