AI OnAI Off
Feb 03, 2020
May 15, 2020
CMS Core
Closed, Fixed and tested
Goal
You want to change via code a block added to a ContentArea field.
Steps to reproduce
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);