Block in published page has “This item is not used anywhere.”

Vote:
 

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.

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 in 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?

productPageClone is found like this:

var contentTypes = _contentTypeRepository.List();

var productPageType = contentTypes.Where(x => x.DisplayName != null && x.Name == "ProductPage").FirstOrDefault();

var criterias = new PropertyCriteriaCollection
{
// Find pages of a specific page type
new PropertyCriteria()
{
Name = "PageTypeID",
Condition = CompareCondition.Equal,
Required = true,
Type = PropertyDataType.PageType,
Value = productPageType.ID.ToString()
}
};

// Search pages under root since more than one startpage

var languagesInSite = ServiceLocator.Current.GetInstance<ILanguageBranchRepository>().ListEnabled();

var allPages = new List<PageData>();

foreach (var lang in languagesInSite)
{
// using findallpageswithcriteria to get unpublished pages and bypass security
var pages = DataFactory.Instance.FindAllPagesWithCriteria(
PageReference.RootPage,
criterias,
lang.LanguageID,
new LanguageSelector(lang.LanguageID)
);

allPages.AddRange(pages.ToList());
}
allPages = allPages.OrderBy(x => x.ContentLink.ID).ToList();


// findallpageswithcritera gets pages in wastebasket, filter these out
var pagesNotInWasteBasket = allPages.Where(x => !x.IsDeleted);

for (int i = 0; i < pagesNotInWasteBasket.Count(); i++)
{
var page = pagesNotInWasteBasket.ElementAt(i);

if (page is ProductPage)
{
var productPage = page as ProductPage;
var versionsInLanguage = _contentVersionRepository.List(productPage.ContentLink, productPage.Language.Name);

// get published version if it exists
var publishedVersion = versionsInLanguage.Where(x => x.Status == VersionStatus.Published).FirstOrDefault();

// get latest saved draft
var latestSavedVersion = versionsInLanguage.OrderByDescending(x => x.Saved).FirstOrDefault();

if (publishedVersion != null && publishedVersion.ContentLink.WorkID == latestSavedVersion.ContentLink.WorkID)
{
var publishedPage = _contentRepository.Get<ProductPage>(publishedVersion.ContentLink);
MigrateProperties(publishedPage, true, publishedVersion.IsCommonDraft);
}
else if (publishedVersion != null && publishedVersion.ContentLink.WorkID < latestSavedVersion.ContentLink.WorkID)
{
var publishedPage = _contentRepository.Get<ProductPage>(publishedVersion.ContentLink);
MigrateProperties(publishedPage, true, publishedVersion.IsCommonDraft);

var latestSavedDraftPage = _contentRepository.Get<ProductPage>(latestSavedVersion.ContentLink);
MigrateProperties(latestSavedDraftPage, false, latestSavedVersion.IsCommonDraft);
}
else if (publishedVersion == null)
{
var latestSavedDraftPage = _contentRepository.Get<ProductPage>(latestSavedVersion.ContentLink);
MigrateProperties(latestSavedDraftPage, false, latestSavedVersion.IsCommonDraft);
}
}

#215866
Edited, Jan 16, 2020 9:22
Vote:
 

This is kind of a wild guess, but could you try without specifying ForceCurrentVersion when saving the page?

#215870
Jan 16, 2020 9:50
- Jan 16, 2020 11:14
Yes I tried to use "only" the Publish and/or Save but the problem remained. Thanks anyway!
Vote:
 

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>();

#215874
Jan 16, 2020 11:12
Vote:
 

Agree with @Tomas, also it could help us to see how you're getting productPageClone are you specifying a version etc

#215876
Jan 16, 2020 11:16
- Jan 16, 2020 11:30
Alright, I added at the bottom how I found the productPages that I loop through.
Vote:
 

I find it very strange that it should be necessary to explicit reindex the softlinks.

#215877
Jan 16, 2020 11:24
- Jan 16, 2020 11:26
Yes me too. However if anyone has a similar problem in the future, this might be a solution.

I have never seen this before. It ould be some caching problem in my environment. But the reference between page and block was updated when the page was manually republished. And I guess that is what is simulated with the softlinkrepository.Save()
Vote:
 

Understand this is a bit old, but in case anyone else ends up here:

Possible this is related to this bug that was recently fixed? https://world.episerver.com/documentation/Release-Notes/ReleaseNote/?releaseNoteId=CMS-15332

#223171
May 20, 2020 22:01
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.