1

My requirement is to send an email on the below activities:

  • When someone invoke Manual Sitecore rebuild index
  • Once the manual Sitecore rebuild index completed

As per my knowledge, it can be done by overriding the existing pipeline. If this is true, kindly let me know the pipeline. If not in this way, please let me know the other way.

Thanks

Copied from OP's comment: I want when someone invoke from Control Panel > Indexing Manager.

  • A manual rebuild can be possible in multiple ways, such as programmatically from a page, from control panel >indexing manager and developer tab in content editor, etc. could you please clear when you want this? – Mahendra Shekhawat Nov 15 '19 at 7:33
  • Thanks Mahendra for your comment and time. I want when someone invoke from Control Panel -> Indexing Manager. – Jitendra Nov 15 '19 at 7:36
1

What you need to do is:

  • Add reference to Sitecore.ContentSearch.Client in your code
  • Create new class CustomIndexingManagerWizard inheriting from Sitecore.ContentSearch.Client.Forms.IndexingManagerWizard with code:
namespace My.Assembly.Namespace
{
    class CustomIndexingManagerWizard : Sitecore.ContentSearch.Client.Forms.IndexingManagerWizard
    {
        protected override Sitecore.Jobs.Job FullRebuild(Sitecore.ContentSearch.ISearchIndex index)
        {
            var job = base.FullRebuild(index);
            Sitecore.Diagnostics.Log.Info($"Here you should send your email saying that one started manual '{index.Name}' index rebuild", this);
            System.Web.HttpRuntime.Cache[job.Handle.ToString()] = new object();
            return job;
        }
    }
}
  • Replace the line starting with Sitecore.Diagnostics.Log.Info() with code which sends emails
  • Additionally the code above adds info to HttpRuntime.Cache that this job is a manual index rebuild process
  • Edit sitecore\shell\Applications\ContentSearch\IndexingManager.xml file and set CodeBeside attribute to:
<WizardForm Application="Search/RebuildSearchIndex" CodeBeside="My.Assembly.Namespace.CustomIndexingManagerWizard, My.Assembly.Namespace">
  • Create new class called IndexRebuildCompleted with code:
namespace My.Assembly.Namespace
{
    class IndexRebuildCompleted
    {
        public void SignalEnd(Sitecore.Jobs.JobArgs args)
        {
            Sitecore.Jobs.Job job = args.Job;
            if (System.Web.HttpRuntime.Cache.Remove(job.Handle.ToString()) != null)
            {
                Sitecore.Diagnostics.Log.Info($"Here you should send your email saying that manual '{job.Options.CustomData}' index rebuild is completed", this);
            }
        }
    }
}
  • Replace the line starting with Sitecore.Diagnostics.Log.Info() with code which sends emails
  • This code will be executed for every Sitecore jobs which is completed and will check if that job is registered as manual index rebuild process
  • Create a new config patch file with content:
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <pipelines>
      <job>
        <processor type="My.Assembly.Namespace.IndexRebuildCompleted, My.Assembly.Namespace" method="SignalEnd" />
      </job>
    </pipelines>
  </sitecore>
</configuration>

And that's it. You can rebuild multiple indexes from Control Panel if you want and there will be notification for start and end of every job.

  • Thanks Marek for your input. First part of you solution is working perfectly fine. but the second part SignalEnd method is not getting called once the manual index rebuild process completed. I am thinking we must have to configure it somewhere so that it can run. – Jitendra Nov 17 '19 at 14:23
  • Stack exchange stripped my xml patch file content. See the updated answer now – Marek Musielak Nov 17 '19 at 16:10
  • Thanks Marek!!! I found one better way to track the manual rebuild index completed task. Just wrote a blog for the same. Please have a look: jitendrasitecore.wordpress.com/2019/11/17/… Basically Sitecore provide a Finished event for Job. So, i used this event to track. – Jitendra Nov 17 '19 at 16:15
0

If this is just for the indexing manager in the control panel, I think you can go with updating the IndexingManager.xml file under \sitecore\shell\Applications\ContentSearch. This file is responsible for showing the indexing wizard and the rebuild button click. This file use CodeBeside attribute Sitecore.ContentSearch.Client.Forms.IndexingManagerWizard from the Sitecore.ContentSearch.Client assembly for WizardForm - enter image description here

you can use your custom code for this file and can override the existing functionality with your send email code.

public class CustomIndexingManagerWizard : IndexingManagerWizard
{
    protected override void ActivePageChanged(string page, string oldPage)
    {
        Assert.ArgumentNotNull((object)page, nameof(page));
        Assert.ArgumentNotNull((object)oldPage, nameof(oldPage));
        base.ActivePageChanged(page, oldPage);
        this.NextButton.Header = "Next";
        if (page == "Setup")
            this.NextButton.Header = "Rebuild";
        if (!(page == "Rebuilding"))
            return;
        this.NextButton.Disabled = true;
        this.BackButton.Disabled = true;
        this.CancelButton.Disabled = false;
        // send email code for StartRebuilding
        SheerResponse.Timer("StartRebuilding", 10);
        // send email code for Rebuilding complete
    }
}

I think the above code is not a good example because it will not give you a good output such as which indexes are rebuilt, is there any problem during the index rebuild and other indexing information. but in the current implementation, you can see StartRebuilding as a protected method and this is responsible for starting indexing rebuild based on the checkbox selection. you can easily change this code as per your requirement and can get other information as well.

  • SheerResponse.Timer("StartRebuilding", 10); is not a simple sync call. Your // send email code for Rebuilding complete code will be called even before index rebuilding start. – Marek Musielak Nov 15 '19 at 9:38
  • Yes @MarekMusielak, you are correct. I just tested this code by adding log.info and it was printing it just after strating index. But I think a better idea is change the startingrebuilding function. – Mahendra Shekhawat Nov 15 '19 at 9:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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