首页 > 其他 > 详细

SharePoint 2010/2013 在某块代码段中临时禁用触发event handler(receiver)

时间:2014-04-03 05:15:51      阅读:442      评论:0      收藏:0      [点我收藏+]

本文讲述如何在SharePoint 2010/2013的解决方案中的某块代码段中临时禁用触发event handler(receiver):

新建类DisabledItemEventsScope:

/// <summary>
///   Disable event firing while item update. Should be used for SharePoint 2010
///   only.
/// </summary>
public class DisabledItemEventsScope : SPItemEventReceiver, IDisposable
{
    private readonly bool oldValue;
 
    /// <summary>
    /// Initializes a new instance of the <see cref="DisabledItemEventsScope"/>
    /// class.
    ///   Default constructor.
    /// </summary>
    public DisabledItemEventsScope()
    {
        oldValue = EventFiringEnabled;
        EventFiringEnabled = false;
    }
 
    public void Dispose()
    {
        EventFiringEnabled = oldValue;
    }
}

使用DisabledItemEventsScope的实例代码如下:

/// <summary>
///   Extension methods for <c>SPListItem</c>.
/// </summary>
public static class SPListItemExtensions
{
    /// <summary>
    ///   Provides ability to update list item without firing event receiver.
    /// </summary>
    /// <param name="listItem">SharePoint list item instance.</param>
    /// <param name="fireEvents">Disables firing event receiver while updating
    /// item.</param>
    public static void Update(this SPListItem listItem, bool fireEvents)
    {
        if (fireEvents == false)
        {
            using (new DisabledItemEventsScope())
            {
                listItem.Update();
            }
        }
        else
        {
            listItem.Update();
        }
    }
}


SharePoint 2010/2013 在某块代码段中临时禁用触发event handler(receiver),布布扣,bubuko.com

SharePoint 2010/2013 在某块代码段中临时禁用触发event handler(receiver)

原文:http://blog.csdn.net/abrahamcheng/article/details/22779265

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!