Skip to content

Instantly share code, notes, and snippets.

@gregwiechec
Created October 5, 2015 21:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gregwiechec/be9d1ed8ff0fd9671cfc to your computer and use it in GitHub Desktop.
Save gregwiechec/be9d1ed8ff0fd9671cfc to your computer and use it in GitHub Desktop.
EPiServer Admin plugin - Reindex Soft Links
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SoftLinkIndexerTool.aspx.cs" Inherits="ChildrenGridView.Plugins.SoftLinkIndexerTool" %>
<asp:Content ContentPlaceHolderID="MainRegion" runat="server">
<asp:Button runat="server" Text="Run" OnClick="OnClick"/>
<asp:Label ID="lblResult" runat="server" Text=""></asp:Label>
</asp:Content>
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using EPiServer;
using EPiServer.Core;
using EPiServer.DataAbstraction;
using EPiServer.PlugIn;
using EPiServer.ServiceLocation;
using EPiServer.Shell.WebForms;
namespace ChildrenGridView.Plugins
{
[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;
}
}
}
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace ChildrenGridView.Plugins {
public partial class SoftLinkIndexerTool {
/// <summary>
/// lblResult control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.Label lblResult;
}
}
@mw-tomas
Copy link

just wondering, what is the purpose of this loop:
foreach (var existingLanguage in localizable.ExistingLanguages)
{
children = this._contentRepository.GetChildren(content.ContentLink, existingLanguage);
}

if you never use the value of children inbetween ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment