If you decompile and have a look at the Sitecore.Data.Items.ItemEditing
class, in Sitecore.Kernel, you will find that all the EndEdit(bool)
method does (and the relevant parts of its overload methods do) is essentially wrap the the call to ItemManager.SaveItem
with an EventDisabler
.
Solution 1:
To apply the same technique in order to make sure that your new item only indexes once for the entire creation and editing process, instead once for creation and once for editing, you could do the following:
using (new EventDisabler())
{
var newItem = parent.Add(name, templateId);
}
newItem.Editing.BeginEdit();
newItem.Fields["Foo"].Value = fooFieldValue;
newItem.Editing.EndEdit();
The problem with this solution, however, is that it will disable all events on create, not just indexing events. What if you have item:created
or item:added
events?
Solution 2:
An an alternative, you could do the following:
IndexCustodian.PauseIndexing();
var newItem = parent.Add(name, templateId);
IndexCustodian.ResumeIndexing();
newItem.Editing.BeginEdit();
newItem.Fields["Foo"].Value = fooFieldValue;
newItem.Editing.EndEdit();
The problem here is that when you call PauseIndexing
you are actually pausing indexing for the entire site. Sure, you're doing it for a very short amount of time, but you are still setting yourself up for failure.
Solution 3:
Another alternative is to try something like the following:
using (new BulkUpdateContext())
{
var newItem = parent.Add(name, templateId);
newItem.Editing.BeginEdit();
newItem.Fields["Foo"].Value = fooFieldValue;
newItem.Editing.EndEdit();
}
This should do what you are looking for, except that it won't work as expected if you want to add child/descendant items to your item or one of its descendants (if adding from a branch template) in the same closure. By example, the following will not work as expected, because the parent, newItem
, for the childItem
won't be available when the childItem
is added:
using (new BulkUpdateContext())
{
var newItem = parent.Add(name, templateId);
newItem.Editing.BeginEdit();
newItem.Fields["Foo"].Value = fooFieldValue;
newItem.Editing.EndEdit();
var newChildItem = newItem.Add(childName, childTemplateId);
...
}
Conclusion:
Unfortunately, I don't think there is a way to temporarily disable indexing for a particular item or for an item creation without using one of the above. Hopefully, one of them will fit your needs.