无法取消在列表项事件接收器ItemDeleting方法缺失CMSPUBLISHINGSITE#2
-
18-09-2019 - |
题
使用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;
}
}
}
解决方案
我会建议你使用异常作为你的业务逻辑的一部分,不是。例外是昂贵的并且应当只在没有被正常逻辑处理例外的情况下被使用。结果 相反,使用这样的:
public override void ItemDeleting(SPItemEventProperties properties)
{
if (properties.UserLoginName.ToLower().CompareTo("sharepoint\\system") != 0)
{
properties.Cancel = true;
properties.ErrorMessage = "Some error has occured....";
}
}
和,顺便说一句,你的事件处理程序这可能是你看到此行为您所遇到的在合理范围内引发其他异常。
不隶属于 StackOverflow