Архив Октябрь, 2013

В интернетах есть несколько вариантов как к примеру обновить элемент без старта привязанных к нему событий, но они либо не очень работают, либо работают в определённых контекстах. Вот ниже способ, который работает при обращении к элементам даже из консольного приложения.

Это extension-класс для SPListItem. Основа это пост на stackoverflow , я лишь добавил от себя ещё пару функций для работы с файлами — добавление файла и функцию CopyTo, для «тихого» копирования файла.

using Microsoft.SharePoint;

public static class SPListItemExtensions
{
    /// <summary>
    /// Provides ability to update list item without firing event receiver.
    /// </summary>
    /// <param name="item">list item</param>
    /// <param name="doNotFireEvents">Disables firing event receiver while updating item.</param>
    public static void Update(this SPListItem item, bool doNotFireEvents)
    {
        SPItemEventReceiverHandling rh = new SPItemEventReceiverHandling();
        if (doNotFireEvents)
        {
            try
            {
                rh.DisableEventFiring();
                item.Update();
            }
            finally
            {
                rh.EnableEventFiring();
            }
        }
        else
        {
            item.Update();
        }
    }

    /// <summary>
    /// Provides ability to update list item without firing event receiver.
    /// </summary>
    public static void SystemUpdate(this SPListItem item, bool incrementListItemVersion, bool doNotFireEvents)
    {
        SPItemEventReceiverHandling rh = new SPItemEventReceiverHandling();
        if (doNotFireEvents)
        {
            try
            {
                rh.DisableEventFiring();
                item.SystemUpdate(incrementListItemVersion);
            }
            finally
            {
                rh.EnableEventFiring();
            }
        }
        else
        {
            item.SystemUpdate(incrementListItemVersion);
        }
    }

    /// <summary>
    /// Provides ability to copy file from list item without firing event receiver.
    /// </summary>
    public static void CopyTo(this SPFile file, string strNewUrl, bool bOverWrite, bool doNotFireEvents)
    {
        SPItemEventReceiverHandling rh = new SPItemEventReceiverHandling();
        if (doNotFireEvents)
        {
            try
            {
                rh.DisableEventFiring();
                file.CopyTo(strNewUrl, bOverWrite);
            }
            finally
            {
                rh.EnableEventFiring();
            }
        }
        else
        {
            file.CopyTo(strNewUrl, bOverWrite);
        }
    }


    /// <summary>
    /// Provides ability to update list item without firing event receiver.
    /// </summary>
    public static void SystemUpdate(this SPListItem item, bool doNotFireEvents)
    {
        SPItemEventReceiverHandling rh = new SPItemEventReceiverHandling();
        if (doNotFireEvents)
        {
            try
            {
                rh.DisableEventFiring();
                item.SystemUpdate();
            }
            finally
            {
                rh.EnableEventFiring();
            }
        }
        else
        {
            item.SystemUpdate();
        }
    }

    /// <summary>
    /// Provides ability to add file to sharepoint library without firing event receiver.
    /// </summary>
    public static SPFile AddFile(SPFileCollection spFileCollection, string destPathToFile, byte[] binFileData, bool overwrite, bool doNotFireEvents)
    {
        SPItemEventReceiverHandling rh = new SPItemEventReceiverHandling();
        if (doNotFireEvents)
        {
            try
            {
                rh.DisableEventFiring();
                SPFile addedFile = spFileCollection.Add(destPathToFile, binFileData, overwrite);
                return addedFile;
            }
            finally
            {
                rh.EnableEventFiring();
            }
        }
        else
        {
            SPFile addedFile = spFileCollection.Add(destPathToFile, binFileData, overwrite);
            return addedFile;
        }
    }

    private class SPItemEventReceiverHandling : SPItemEventReceiver
    {
        public SPItemEventReceiverHandling() { }

        new public void DisableEventFiring()
        {
#pragma warning disable 612,618
            base.DisableEventFiring();
#pragma warning restore 612,618
        }

        new public void EnableEventFiring()
        {
#pragma warning disable 612,618
            base.EnableEventFiring();
#pragma warning restore 612,618
        }
    }
}