Orchard Hungary is an archive

This site is now an archive. We'll keep it so the content is accessible, but we won't update it anymore. However, we frequently publish Orchard-related news on our other site Orchard Dojo, so be sure to check that out instead. And if you're interested in news about our Orchard development company, Lombiq, check out our website.

Changing the content type of existing content items

Tags: Orchard, development, content types

So you've got a bunch of Page content items in your Orchard instance, but nowadays Orchard is getting popular and everybody has Page content items. But you started to use Orchard before it got cool and Pages are now too mainstream. How can you make your existing Pages unique again? Well, you could change them to use you very own content type, BakelitePage!

First, set up the BakelitePage content type and make sure that it has every content part attached that Page has. Then you have to morph all of the existing Page content items to BakelitePage with the following code:

public class MarkdownPageMigrationController : Controller
    private readonly IContentManager _contentManager;
    private readonly IRepository<ContentTypeRecord> _contentTypeRepository;

    public MarkdownPageMigrationController(IContentManager contentManager, IRepository<ContentTypeRecord> contentTypeRepository)
        _contentManager = contentManager;
        _contentTypeRepository = contentTypeRepository;

    public string Index()
        // Creating an item is needed to make sure that the content type record is properly saved.
        // You don't need this if you can be sure that there were already content itema with the new type created.
        var dummy = _contentManager.New("BakelitePage");

        // Content types have corresponding records in the DB. We just have to swap out which content type record our content
        // items reference.
        var newContentTypeRecord = _contentTypeRepository.Table.SingleOrDefault(record => record.Name == "BakelitePage");
        var pages = _contentManager.Query("Page").List();

        foreach (var page in pages)
            // This is the line where our pages get cool again.
            page.Record.ContentType = newContentTypeRecord;

        return "OK";

Happy getting exciting again!

No Comments