Sean Holmesby

.NET and Sitecore Developer

By

TDS Post Deploy Step – Rebuild Content Search Indexes Script

This post shows how I created a custom TDS Post Deploy Step to rebuild the Content Search Indexes after my deployment in the Launch Sitecore TDS solution.

Hedgehog are currently asking for community contributions to the Post Deploy Step repository, so we can all benefit from each other’s work.

Got an idea for a Post Deploy Step? Let us know.
Getting started is super easy.

My Custom Post Deploy Step:
Rebuild Content Search Indexes

Problem: As a developer, you may have added new fields to a Sitecore data template, or extended Sitecore with a custom index field. TDS makes it easy to get your changes deployed to other environments, however, you can’t begin querying these fields until a full index rebuild is performed. Deploying new code without an index rebuild can result in search listings coming up empty, or worse, broken pages.

Solution: Remove the manual and easy-to-forget task of rebuilding indexes by letting this Post Build step take care of things on your behalf.

Installation: Install the post build step using the instructions. Pass in one or more indexes to rebuild in the form of a comma separated list. (ex: sitecore_master_index,sitecore_web_index). This post build step can take a while to run so you’ll need to make sure TDS accounts for that.

https://github.com/SaintSkeeta/LaunchSitecoreTDS/blob/develop/Source/HedgehogDevelopment.TDS.CustomExtensions/PostBuildProcessors/RebuildContentSearchIndexes.cs

using HedgehogDevelopment.SitecoreProject.PackageInstallPostProcessor.Contracts;
using HedgehogDevelopment.SitecoreProject.PackageInstallPostProcessor.Utils;
using Sitecore.ContentSearch;
using Sitecore.ContentSearch.Maintenance;
using Sitecore.Data;
using Sitecore.Data.Items;
using Sitecore.Data.Managers;
using Sitecore.Globalization;
using Sitecore.Publishing;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
 
namespace HedgehogDevelopment.TDS.CustomExtensions.PostBuildProcessors
{
    public class RebuildContentSearchIndexes : IPostDeployAction
    {
        public void RunPostDeployAction(XDocument deployedItems, IPostDeployActionHost host, string parameter)
        {
            string[] indexNames = this.GetContentSearchIndexes(parameter);
            foreach (var indexName in indexNames)
            {
                var index = ContentSearchManager.GetIndex(indexName.Trim());
 
                if (index == null)
                {
                    host.LogMessage("The Content Search Index with name {0} supplied does not exist.", indexName);
                    continue;
                }
 
                host.LogMessage("Rebuilding index {0}...", indexName);
                IndexCustodian.FullRebuild(index, true);
                host.LogMessage("Rebuild of index {0} complete", indexName);
            }
        }
 
        private string[] GetContentSearchIndexes(string parameters)
        {
            if (string.IsNullOrEmpty(parameters))
            {
                throw new System.InvalidOperationException("Please specify a Sitecore Content Search index in the parameter");
            }
 
            return parameters.Split(new char[]
            {
                ','
            }, System.StringSplitOptions.RemoveEmptyEntries);
        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *