Sitecore 7 create item and update index at the same time

With Sitecore 7, the Sitecore index (based on Lucene.NET) have greatly improved, thus making the index even more usable.

One thing you should know though, is that when you add items to Sitecore using the Sitecore.Data.Items.Item.Add() method, the index is not immediately updated. The index is updated shortly after, but the time varies depending on your index update strategy.
The result is that if you query the index just after you added a new item, it is not a given that the item exists in the index.

There is a solution to this (of course, this is Sitecore). When you have added the new item, you must manually update the search index for this specific item.

I have made an Extension Method to show how this can be done:

using System;
using System.Linq;
using Lucene.Net.Index;
using Sitecore.ContentSearch;
using Sitecore.Data;
using Sitecore.Data.Items;
using Sitecore.Diagnostics;

namespace MyProject
  public static class ItemExtensions
    public static Item AddAndUpdate(this Item item, string name, TemplateID templateID)
      Assert.ArgumentNotNull(item, "item");
      Assert.IsTrue(item.Database.Name.ToLower() == "master", "Could not add item to " + item.Paths.FullPath + ": item is not in the 'master' database");
      Item newItem = item.Add(ItemUtil.ProposeValidItemName(name), templateID);
      Log.Audit("Item create: " + AuditFormatter.FormatItem(newItem), typeof(ItemExtensions));
      var tempItem = (SitecoreIndexableItem)newItem;
      return newItem;


The trick lies within these 2 lines of code:

var tempItem = (SitecoreIndexableItem)newItem;

First the item is converted into a SitecoreIndexableItem, and then the master index is refreshed for this single item.

The extension method can be used like this:

using MyProject;

namespace MyNamespace
  public void AddItem(Item parent, string name, TemplateID templateID)
    Item newItem = parent.AddAndUpdate(name, templateID);

Please note that this will increase the time to create an item substantially, from barely mesureable milliseconds to a full second or so.




About briancaos

Developer at Pentia A/S since 2003. Have developed Web Applications using Sitecore Since Sitecore 4.1.
This entry was posted in .net, c#, Sitecore 7 and tagged , , , , . Bookmark the permalink.

5 Responses to Sitecore 7 create item and update index at the same time

  1. This looks exactly what I need – I am relying perhaps a bit too much on the Search Index, and this is causing all sort of trouble with newly-added Items.

    What about the performance hit of your solution?


  2. Actually nevermind, I read it at the end of your post – feel free to ignore both my comments.


  3. Harsh Baid says:

    I had tried this `item.Database.Engines.HistoryEngine.RegisterItemDeleted(item, item.ParentID);
    item.Database.Engines.HistoryEngine.RegisterItemCreated(item);` but it didn’t helped. Thanks to you `var tempItem = (SitecoreIndexableItem)item;
    ContentSearchManager.GetIndex(Settings.GetSetting(“MySiteName.IndexName”, “sitecore_master_index”)).Refresh(tempItem, IndexingOptions.ForcedIndexing);` works fine. If you can tell the difference that would be great in these two pieces.


  4. phaniav says:

    Hi Brian, What happens to the original item:save index refresh trigger. Does it still fire?


  5. briancaos says:

    Yes, the event handles are still fired.


Leave a Reply

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

You are commenting using your 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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.