1

我有一个自定义列表定义,其中包含一个覆盖 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; }
}

我尝试过的事情:

  • 使用块更新 listItemusing Site/using Web而不是从属性更新项目。
  • 设置 properties.AfterProperties["Is Published?"] 字段。
4

1 回答 1

2

您不应该在同步事件中调用系统更新。事件而不添加其他版本。

如果您想在更新之前更新属性,您可以更改 afterProperties[""] 并且如果更新成功,更改将保持不变。

base.ItemUpdating(properties);
properties.AfterProperties["Is Published"] = true;

顺便说一句,您还可以使用 ListItem.ModerationInformation.Status == SPModerationStatusType.Approved(= 已发布和批准)检索发布状态

依靠 ootb 内部字段将确保您不必弄乱其他事件接收器(当心内容部署运行时等有趣的事情......)并确保状态始终是最新的-日期。

希望它有所帮助。

于 2012-02-23T12:33:52.793 回答