November Happy Hour will be moved to Thursday December 5th.

PuneetGarg
Apr 26, 2022
  1755
(4 votes)

Content Migration from one to another property for live site

In one of my projects I recently faced an issue where we have to change all the standard category properties to IList<ContentReference> and the site was live so we just couldn't take the field and get it over with.

So I create new IList<ContentReference> properties and migrate the data and change the logic. Here are the steps that I followed.

Step To follow:-

    • Create new fields -- for all categories while don't remove old ones once
    • Create a scheduled job that migrates data from the old field to the new field
    • Switch the logic from old to new fields as new fields have multiple categories
    • remove old fields (only after running the migration job)

Step-1:-

We create new Ilist<contentreference> properties for every standard category property.

 

 [Display(
            Name = "Something",
            GroupName = SystemTabNames.Content,
            Order = 10)]
        [Categories]
        public virtual IList<ContentReference> Something { get; set; }

Step-2:-

Here we have to write the code so that content that is already existing on the live site migrates from old to new properties.

Few things that I did while creating a scheduled job:

        • Get all the pages where I changed the property, So for that, I created a generic method.
 private IEnumerable<T> GetDataList<T>()
            where T : ContentData
        {
            var objectType = _contentTypeRepository.Load<T>();
            var usages = _contentModelUsage.ListContentOfContentType(objectType)
                .Select(x => x.ContentLink.ToReferenceWithoutVersion())
                .Distinct()
                .Select(x => _contentLoader.Get<IContentData>(x))
                .OfType<T>()
                .ToList();
            return (IEnumerable<T>)usages;
        }

 

        • After this method call loop through all the pages and create a writeable clone so that we change the data
        • invoke the list and then assign the value to it.
 OnStatusChanged($"Migration start for ChapterDetailPage");

            var chapterDetailPages = GetDataList<ChapterDetailPage>();
            if (chapterDetailPages != null)
            {
                foreach (var cdp in chapterDetailPages)
                {
                    //migrate data
                    var page = (ChapterDetailPage)cdp.CreateWritableClone();
                    page.LocationIdNew = new List<ContentReference>();

                    var hit = _getaCatRep.TryGet(new ContentReference(page.LocationID), out stdCat) ? stdCat : null;
                    if (hit != null)
                    {
                        page.LocationIdNew.Add(hit.ContentLink);
                        _contentRepository.Save(page, SaveAction.Publish, AccessLevel.NoAccess);
                    }
                }
            }

            OnStatusChanged($"Migration end for ChapterDetailPage");
        • Then Save the page using the Content Repository.

Step-3

Change all the existing logic so that pages don't error.

Step-4

The last step will be to remove all old properties.

Apr 26, 2022

Comments

Please login to comment.
Latest blogs
Set Default Culture in Optimizely CMS 12

Take control over culture-specific operations like date and time formatting.

Tomas Hensrud Gulla | Nov 15, 2024 | Syndicated blog

I'm running Optimizely CMS on .NET 9!

It works 🎉

Tomas Hensrud Gulla | Nov 12, 2024 | Syndicated blog

Recraft's image generation with AI-Assistant for Optimizely

Recraft V3 model is outperforming all other models in the image generation space and we are happy to share: Recraft's new model is now available fo...

Luc Gosso (MVP) | Nov 8, 2024 | Syndicated blog

ExcludeDeleted(): Prevent Trashed Content from Appearing in Search Results

Introduction In Optimizely CMS, content that is moved to the trash can still appear in search results if it’s not explicitly excluded using the...

Ashish Rasal | Nov 7, 2024

CMS + CMP + Graph integration

We have just released a new package https://nuget.optimizely.com/package/?id=EPiServer.Cms.WelcomeIntegration.Graph which changes the way CMS fetch...

Bartosz Sekula | Nov 5, 2024

Block type selection doesn't work

Imagine you're trying to create a new block in a specific content area. You click the "Create" link, expecting to see a CMS modal with a list of...

Damian Smutek | Nov 4, 2024 | Syndicated blog