你可以使用一些擴展方法來默默更新項目列表項。
public static class SPListItemExtensions
{
/// <summary>
/// Provides ability to update list item without firing event receiver.
/// </summary>
/// <param name="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>
/// <param name="item"></param>
/// <param name="incrementListItemVersion"></param>
/// <param name="doNotFireEvents">Disables firing event receiver while updating item.</param>
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 update list item without firing event receiver.
/// </summary>
/// <param name="item"></param>
/// <param name="doNotFireEvents">Disables firing event receiver while updating item.</param>
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();
}
}
private class SPItemEventReceiverHandling : SPItemEventReceiver
{
public SPItemEventReceiverHandling() { }
new public void DisableEventFiring()
{
base.DisableEventFiring();
}
new public void EnableEventFiring()
{
base.EnableEventFiring();
}
}
}
這很好 - thx! – 2010-03-18 13:21:50
建議:使用「using」模式實現SPItemEventReceiverHandling,請參閱http://adrianhenke.wordpress.com/2010/01/29/disable-item-events-firing-during-item-update/ – driAn 2010-05-07 08:35:23
有關更多信息,請參閱下面的答案。適用於SP2010的示例和代碼 – 2011-06-28 17:04:59