2

I have a scheduled job that loops through all pages of a certain type and creates a block for each page and puts it in a ContentArea.

if (productPageClone.GeneralContentArea == null)
                {
                    productPageClone.GeneralContentArea = new ContentArea();
                }

var newBlockForArea = _contentRepository.GetDefault<CrossLinkContainerBlock> 
(assetsFolderForPage.ContentLink, productPageClone.Language);
                        (newBlockForArea as IContent).Name = "newCrossLinkContainer";

var blockReference = _contentRepository.Save((newBlockForArea as IContent), SaveAction.Publish, 
AccessLevel.NoAccess);

var newItem = new ContentAreaItem();
newItem.ContentLink = blockReference;
productPageClone.GeneralContentArea.Items.Add(newItem);

When the block is created it is published.

When the page is updated it is either saved or published depending on earlier status.

_contentRepository.Save(productPageClone, SaveAction.ForceCurrentVersion | SaveAction.Publish, 
AccessLevel.NoAccess);`

Later when inspecting the page, the block is in the page's assets folder and the block is in the correct ContentArea and it renders correctly. The only problem is that when I edit the block, it says "This item is not used anywhere."

However, then I republish the page the block is in, and then edit the block, it says "Changes made here will affect at least 1 item" as it should.

I am using Episerver 11.11.2.0

I have run the scheduled job manually each time I've tested this.

Has anyone any idea why this is happening?

2 Answers2

2

I found the solution after reading this page: https://gregwiechec.com/2015/10/reindexing-soft-links/

After page that has the new block has been published, get the page's softLinks and re-index them:

var links = _contentSoftLinkIndexer.GetLinks(productPageClone);

_softLinkRepository.Save(productPageClone.ContentLink.ToReferenceWithoutVersion(), 
productPageClone.Language, links, false);

Softlink-tools are imported like this:

private IContentSoftLinkRepository _softLinkRepository = 
ServiceLocator.Current.GetInstance<IContentSoftLinkRepository>();

private ContentSoftLinkIndexer _contentSoftLinkIndexer = 
ServiceLocator.Current.GetInstance<ContentSoftLinkIndexer>();
  • Nice catch. You should probably put that in an event for better automation! – Eric Herlitz Jan 16 '20 at 12:48
  • 2
    I think this part from the link you reference is really vital and should be copied in the answer `We figured out that the problem is related to Soft Links index. When saving the content with ForceCurrentVersion flag, the publish event is not fired and references are not reindexed.` – Lanorkin Jan 17 '20 at 08:15
1

This will occur if your content area is null

Try the following

// Before adding the ContentAreaItem
if(productPageClone.GeneralContentArea == null) 
{
    productPageClone.GeneralContentArea = new ContentArea();
}

productPageClone.GeneralContentArea.Items.Add(newItem);
Eric Herlitz
  • 25,354
  • 27
  • 113
  • 157
  • I forgot to add that in the description. Yes I already have that code: if (productPageClone.GeneralContentArea == null) { productPageClone.GeneralContentArea = new ContentArea(); } Thanks! And I will update the text. – Hannes Carleson Jan 16 '20 at 10:00
  • The problem is that the page gets a reference to the block first when the page is manually published. As of now, I am investigating if this has to do with SoftLinks not updating correctly. – Hannes Carleson Jan 16 '20 at 10:07