我有一个自定义列表定义,其中包含一个覆盖 ItemUpdating 的事件接收器。该列表已打开内容审批,以及创建主要和次要版本。
如果项目正在审批中,我想设置一个布尔字段(Is Published?),而不影响版本和审批状态。我知道SystemUpdate(false)应该这样做,但是它不会保留 bool 值。如果我使用Update()或SystemUpdate(),该值将被保留,但它不会将批准状态设置为Approved并引发以下错误:
[用户] 在 [日期] 修改了文件 [文件名]。
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; }
}
我尝试过的事情:
- 使用块更新 listItem
using Site/using Web
而不是从属性更新项目。 - 设置 properties.AfterProperties["Is Published?"] 字段。