This is kind of a wild guess, but could you try without specifying ForceCurrentVersion when saving the page?
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>();
Agree with @Tomas, also it could help us to see how you're getting productPageClone are you specifying a version etc
I find it very strange that it should be necessary to explicit reindex the softlinks.
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
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);
}
}