Is there a way to stop OnItemSaving events?

I managed to stop events OnItemSaved

triggered with

item.Editing.EndEdit(false,true);

      

It raises events anyway OnItemSaving

. Is there a way to disable this as well?

+3


source to share


2 answers


You can do it with EventDisabler

:

using (new EventDisabler())
{
    item.Editing.BeginEdit();
    item.Editing.EndEdit();
}

      



Found here: Temporarily Disabling Events Via Sitecore API

+3


source


To expand on @Ruud van Falier's answer, it's worth noting that EventDisabler

, like SecurityDisable

, inherits from IDisposable

. Thus, you can create a property or local variable that is EventDisabler

, and Sitecore events will be disabled from the time the variable or property is initialized until the time the method is called Dispose()

.

For example, in the following class, events will be disabled from the time the method is called Foo()

until the time the method is called Bar()

.



public class SomeClass 
{
    private EventDisabler _eventDisabler;

    ...

    public void ToggleEventDisabler(bool eventDisablerOn)
    {
        _eventDisabler = eventDisablerOn ? new EventDisabler() : null;
    }

    public void Foo() 
    {
        ToggleEventDisabler(true); //turn on the event disabler

        ...do stuff...
    }

    public void Bar() 
    {
        ...do stuff...

        if (_eventDisabler != null) 
        {
            _eventDisabler.Dispose();
        }
    }
}

      

0


source







All Articles