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

tblContentSoftLink data not updated when changing ContentAreaItem's Reference by code

Found in

EPiServer.CMS.Core 11.11.3

Fixed in

EPiServer.CMS.Core 11.15.1

(Or a related package)

Created

Feb 03, 2020

Updated

May 15, 2020

Area

CMS Core

State

Closed, Fixed and tested


Description

Goal
You want to change via code a block added to a ContentArea field.

Steps to reproduce

  1. Clone the page.
  2. Update the contentarea.
  3. Use the contentRepository.Save() method.

var contentTypeRepository = ServiceLocator.Current.GetInstance<IContentTypeRepository>();
            var _contentRepository = ServiceLocator.Current.GetInstance<IContentRepository>();
            var pageId = 114;
            var page = (StandardPage) _contentRepository.Get<StandardPage>(new ContentReference(pageId)).CreateWritableClone();
 
            var contentArea = page.MainContentArea.CreateWritableClone();
            contentArea.Items.Clear();
 
            var contentItem = new ContentAreaItem() {
                ContentLink = new ContentReference(blockId)
 
            };
            contentArea.Items.Add(contentItem);
            page.MainContentArea = contentArea;
            var newReference = _contentRepository.Save(page, SaveAction.Publish, EPiServer.Security.AccessLevel.NoAccess);

4. The new Block item is changed in CMS Editor UI, but the change is not sent to the database > tblContentSoftlink table.

You must add the below code to update the Softlink:

//update soft link
            var contentSoftLinkIndexer = ServiceLocator.Current.GetInstance<ContentSoftLinkIndexer>();
            var contentSoftLinkRepo = ServiceLocator.Current.GetInstance<IContentSoftLinkRepository>();
 
            var links = contentSoftLinkIndexer.GetLinks(page);
            contentSoftLinkRepo.Save(page.ContentLink.ToReferenceWithoutVersion(), null, links, false);
            //Clearing the cache for the page has to be done as well:
            var contentCacheRemover = ServiceLocator.Current.GetInstance<EPiServer.IContentCacheRemover>();
            contentCacheRemover.Remove(page.ContentLink);
            contentCacheRemover.Remove(newReference);