1

使用 SharePoint 2010 RC 我在使用事件接收器取消删除列表项时遇到问题。我的代码正在触发、设置 SPItemEventProperties 的取消属性、设置错误消息并将错误返回给调用线程。这种方法在添加/更新方法中运行良好,但是,在删除方法中使用时,我可以在调试器中看到代码触发,但该项目仍被移动到站点的回收站。

此外,我在从 stsadm 的“CMSPUBLISHINGSITE#2”模板创建的站点中看到了这种行为,而不是从通过管理中心从“团队站点”模板创建的站点中看到的。

行为不端的代码如下:

public override void ItemDeleting(SPItemEventProperties properties)
{
    if (!(properties.UserLoginName == "SHAREPOINT\\system"))
    {
        try
        {
            throw new CreatorIdAliasingException("Please contact support if you feel a release web site has been inappropriately assigned to your organization.");
        }
        catch (CreatorIdAliasingException ex)
        {
            properties.Cancel = true;
            properties.ErrorMessage = ex.ToString();
            properties.InvalidateListItem();
            throw;
        }
    }
}

作为参考,ItemAdding 方法中包含相同的代码并按预期工作。

public override void ItemAdding(SPItemEventProperties properties)
        {
            base.ItemAdding(properties);
            if (!(properties.UserLoginName == "SHAREPOINT\\system"))
            {
                try
                {
                    throw new InvalidCreatorIdException("Please contact support to add a known URL to your list of release web sites.");
                }
                catch (InvalidCreatorIdException ex)
                {
                    properties.Cancel = true;
                    properties.ErrorMessage = ex.ToString();
                    properties.InvalidateListItem();
                    throw;
                }
            }
        }
4

1 回答 1

2

我建议您不要将异常用作业务逻辑的一部分。异常是昂贵的,只应在正常逻辑无法处理的异常情况下使用。
相反,使用这样的东西:

public override void ItemDeleting(SPItemEventProperties properties)
{
  if (properties.UserLoginName.ToLower().CompareTo("sharepoint\\system") != 0)
  {
    properties.Cancel = true;
    properties.ErrorMessage = "Some error has occured....";
  }
}

而且,顺便说一句,您在事件处理程序中抛出了一个额外的异常,这可能是您看到您正在经历的这种行为的原因。

于 2010-03-09T13:15:30.467 回答