我有一個自定義列表定義,其中包含一個覆蓋ItemUpdating的事件接收器。該列表有內容批准開啓,連同創建主要和次要版本。在ItemUpdating中設置SPListItem字段不是持久的SystemUpdate(false)
如果項目被批准,我想設置一個布爾型字段(發佈?),而不會影響版本和審批狀態。據我所知系統更新(假)是假設這樣做,但它不會堅持布爾值。如果我使用更新()或SystemUpdate(),該值仍然存在,但它不審批狀態設置爲批准並引發以下錯誤:
The file [filename] has been modified by [user] on [date].
public override void ItemUpdating(SPItemEventProperties properties)
{
base.ItemUpdating(properties);
EventFiringEnabled = false;
try
{
if (IsChangingToApproved(properties))
{
if (!Validate(properties))
{// This person can't approve
properties.ErrorMessage = "You don't have appropriate permissions.";
properties.Status = SPEventReceiverStatus.CancelWithError;
properties.Cancel = true;
}
else
{// Set the IsPublished flag to true
var isPublishedField = properties.List.Fields["Is Published?"];
if (isPublishedField != null)
{
properties.ListItem[isPublishedField.InternalName] = true;
// Doesn't update bool, ItemUpdating event functions normally
properties.ListItem.SystemUpdate(false);
// Updates bool, but ItemUpdating event does not complete
//properties.ListItem.Update();
//properties.ListItem.SystemUpdate();
}
}
}
}
catch (Exception ex) { return; }
finally { EventFiringEnabled = true; }
}
事情我已經嘗試過:
- 使用
using Site/using Web
塊更新listItem,而不是從屬性更新項目。 - 設置properties.AfterProperties [「Is Published?」]字段。
在同步事件中更新您是正確的。我使用異步ItemUpdated事件,因爲設置AfterProperties不適合我。我正在標記你的答案是正確的。 – rboone 2012-02-23 19:41:20
很高興我能夠幫助:)接收器並不容易掌握,即使經過幾年與SharePoint的合作,我仍然每天都在學習。祝你的項目好運。 – 2012-02-23 20:14:41