Herlitz

Episerver find DeleteIndexItems routine

Feb 12th, 2019
117
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /// <summary>
  2. /// Remove from Episerver Find, use cascade to remove any descendent as well
  3. /// </summary>
  4. /// <param name="entryPoint"></param>
  5. /// <param name="cascade"></param>
  6. /// <returns></returns>
  7. public static List<string> DeleteIndexItems(this ContentReference entryPoint, bool cascade)
  8. {
  9.     // Todo! Make Valdis safe, use injection
  10.     var loader = ServiceLocator.Current.GetInstance<IContentLoader>();
  11.  
  12.     // Message class to keep track of whats happening
  13.     var messages = new List<string>();
  14.  
  15.     // Get all indexable languages from Find
  16.     Languages languages = SearchClient.Instance.Settings.Languages;
  17.  
  18.     // Remove all current instances of all languages below the selected content node
  19.     foreach (var lang in languages)
  20.     {
  21.         if (cascade)
  22.         {
  23.             var descendents = loader.GetDescendents(entryPoint);
  24.  
  25.             foreach (ContentReference descendent in descendents)
  26.             {
  27.                 ContentIndexer.Instance.RemoveFromIndex(descendent, false, lang.FieldSuffix);
  28.                 messages.Add($"Removing descendent: {descendent.ID}, lang {lang.FieldSuffix}");
  29.             }
  30.         }
  31.  
  32.         // Try delete the entrypoint
  33.         var entryTest = loader.Get<IContent>(entryPoint, new CultureInfo(lang.FieldSuffix));
  34.         if (entryTest != null)
  35.         {
  36.             var delRes = ContentIndexer.Instance.Delete(entryTest);
  37.             messages.Add($"Deleting entrypoint: Found {delRes.FirstOrDefault().Found}, Ok = {delRes.FirstOrDefault().Ok}, Id = {delRes.FirstOrDefault().Id}");
  38.         }
  39.  
  40.     }
  41.  
  42.     messages.Insert(0, $"Done removing from index");
  43.  
  44.     return messages;
  45. }
RAW Paste Data