Re-Index a Sitecore Content Tree Programmatically

This is going to be a very short blog and it has nothing to do with Youtube Connector :). So, here is the scenario. I am working on a Sitecore story on Importing Items from an external source. Basically, reading data from the external source and creating items in Sitecore. For improving the performance we had to pause/disable indexing, disable raising events etc. After the Items are created we resume/enable the indexes again. This is all good, but the problem is, the items do not get picked up by the index crawler because the indexing is disabled and events are suppressed. So the search cannot find the items just added by the Import Process, until we rebuild the indexes.

There are two ways we can fix this issue, we can rebuild the index programmatically or we can programmatically re-index the content tree, where we have added the new items. The first option takes forever, based on how many indexes we have, where roots are set for those indexes and how many items are there in Sitecore. The second option is quick, because it only crawls through the items we added to Sitecore. In Sitecore Content Editor tree re-indexing can be done manually by selecting the root item of the tree and then clicking on the ‘Re-index Tree’ button on the Developer tab.

Re-Indexing Tree

I need to do it programatically.

It’s very easy. All I need to have the root item of the tree that I am re-indexing, create an object of SitecoreIndexableItem with the root item and then call the Refresh method of the implementation of ISearchIndex. Here is the code.

public class ItemImporter
{
  public Item RootItem { get;set; }

  public void Import()
  {
    foreach (ISearchIndex searchIndex in ContentSearchManager.Indexes)
      searchIndex.PauseIndexing();
    Settings.Indexing.Enabled = false;
    using (new EventDisabler())
    {
      //Code that Imports data from external system and creates Items
    }
    Settings.Indexing.Enabled = true;
    foreach (ISearchIndex searchIndex in ContentSearchManager.Indexes)
    {
      searchIndex.ResumeIndexing();
      searchIndex.Refresh(new SitecoreIndexableItem(this.RootItem));
    }
  }
}

That’s it! Happy Sitecoring.

Advertisements

About Himadri Chakrabarti

I am a software developer architect and a Sitecore MVP. My professional interest is everything and anything related to Software Architecture, .NET, Sitecore, Node.js, NoSQL etc. Outside of my profession, I am a hobbyist photographer. Link to my photography site http://himadriphotography.com/
This entry was posted in Indexing, Sitecore and tagged , , . Bookmark the permalink.

One Response to Re-Index a Sitecore Content Tree Programmatically

  1. Pingback: Custom Synchronization Provider for Sitecore Commerce Connect | Himadri's Technical Blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s