For one of our customer websites we developed new features, but also get rid of some unused functionality. During the implementation we had to change some properties types and move them into different models. We didn’t want to create drafts for published pages, so for saving content we used GetForceCurrentVersionSaveAction extension method.
1 2 3 4 |
PageData page = //... get page; var pageClone = page.CreateWritableClone(); //... do the content migration this.contentRepository.Save(pageClone, pageClone.GetForceCurrentVersionSaveAction()); |
After we start testing, it turns out that for some blocks the “This item is not used anywhere” was displayed. We knew that each of those blocks were used at least on one page. The information in the top bar is important for editors, because they use it to determinate where the block is used.
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.
I found the blog post about rebuilding Soft Link index. The code was written for EPiserver 6 platform and used LazyIndexer class. This class is still available in new EPiServer, but it’s mark as obsolete. Executing IndexPage method has absolutely no effect because it looks like the body is empty 🙂
1 |
EPiServer.LazyIndexer.IndexPage(pageId); |
In current platform version the ContentSoftLinkRepository and ContentSoftLinkIndexer should be used when rebuilding index.
1 2 |
var links = _contentSoftLinkIndexer.GetLinks(content); this._softLinkRepository.Save(content.ContentLink.ToReferenceWithoutVersion(), null, links); |
Reindexing soft links admin plugin
To repair all block and page references we prepared Admin plugin. It iterates through all pages and reset Soft Links.
After running the tool, all content was fixed and now the top ribbon is showing the correct information.
Full source code is available on Gist
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
[GuiPlugIn(DisplayName = "Reindex Soft Links", Description = Description, Area = PlugInArea.AdminMenu, Url = "~/Plugins/SoftLinkIndexerTool.aspx")] public partial class SoftLinkIndexerTool : WebFormsBase { public const string Description = "Used to reindex all content"; private readonly IContentRepository _contentRepository = ServiceLocator.Current.GetInstance<IContentRepository>(); private readonly ContentSoftLinkRepository _softLinkRepository = ServiceLocator.Current.GetInstance<ContentSoftLinkRepository>(); private readonly ContentSoftLinkIndexer _contentSoftLinkIndexer = ServiceLocator.Current.GetInstance<ContentSoftLinkIndexer>(); protected override void OnPreInit(EventArgs e) { base.OnPreInit(e); this.MasterPageFile = UriSupport.ResolveUrlFromUIBySettings("MasterPages/EPiServerUI.master"); this.SystemMessageContainer.Heading = "Reindex Soft Links"; this.SystemMessageContainer.Description = Description; } protected void OnClick(object sender, EventArgs e) { var stopwatch = Stopwatch.StartNew(); var numberOfPages = this.UpdateSoftLinks(this._contentRepository.Get<IContent>(ContentReference.RootPage)); this.lblResult.Text = $"Total time: {stopwatch.Elapsed.ToString("g")}. Number of links found: {numberOfPages}"; } public int UpdateSoftLinks(IContent content) { var links = _contentSoftLinkIndexer.GetLinks(content); var localizable = content as ILocalizable; IEnumerable<IContent> children = null; if (localizable != null) { this._softLinkRepository.Save(content.ContentLink.ToReferenceWithoutVersion(), localizable.Language, links); foreach (var existingLanguage in localizable.ExistingLanguages) { children = this._contentRepository.GetChildren<IContent>(content.ContentLink, existingLanguage); } } else { this._softLinkRepository.Save(content.ContentLink.ToReferenceWithoutVersion(), null, links); children = this._contentRepository.GetChildren<IContent>(content.ContentLink, CultureInfo.InvariantCulture); } var numberOfLinks = 0; foreach (var childContent in children) { numberOfLinks += UpdateSoftLinks(childContent); } return links.Count + numberOfLinks; } } |