1

I use an Azure DevOps pipeline to refresh test environment databases (master, web, core) with production data.

I want to make the re-indexing of the databases part of the Pipeline, is there a way to start the reindexing of a database via an API call?

1 Answer 1

1

You would need to create your own API to do this, then call the sitecore API from within.

Something like this should work:

Controller

namespace YourNameSpace.Controllers
{
    public class YourController : SitecoreController
    {
        public ActionResult TriggerIndexRebuild(string indexName, string token)
        {
            bool success=false;

            // code to confirm token against config
            
            try
            {
                IndexCustodian.FullRebuild(ContentSearchManager.GetIndex(indexName), true);
                IndexCustodian.RebuildAll();
                success=true;
            }
            catch(Exception ex)
            {
                // log the exception
            }

            return Json(success, JsonRequestBehavior.AllowGet);
        }
    }
}

Class to register route

namespace YourNameSpace
{
    public class RegisterCustomRoute
    {
        public virtual void Process(PipelineArgs args)
        {
            Register();
        }

        public static void Register()
        {
            RouteTable.Routes.MapRoute("SearchReport", "api/sitecore/triggerindexrebuild/{indexName}/{token}", new { controller = "Your", action = "TriggerIndexRebuild" });
        }

    }
}

Config to register route

<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <pipelines>
      <initialize>
        <processor
          type="YourNameSpace.RegisterCustomRoute,YourNameSpace"
          patch:before="processor[@type='Sitecore.Mvc.Pipelines.Loader.InitializeRoutes, Sitecore.Mvc']"
         />
      </initialize>
    </pipelines>
  </sitecore>
</configuration>

Once setup, you should then just be able to call:

https://your-sitecore-instance/api/sitecore/triggerindexrebuild/sitecore-web-index/xxxx-xxxx-xxxx

Sitecore Reference here:

https://doc.sitecore.com/xp/en/developers/103/platform-administration-and-architecture/rebuild-search-indexes.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.