处理ItemUpdating事件接收器多次触发
-
29-09-2020 - |
题
在文档库的情况下,项目更新事件接收器触发两次的原因是因为签入/签出。
如果我们在这个项目更新事件中更新当前的项目值,它也可以在doc库/列表的情况下触发两次。
我们如何通过代码处理这两种情况?
解决方案
对于当你是触发第二个的人时的场景 ItemUpdating
通过更改项目值,您可以使用 EventFiringEnabled
参数(你应该使用一个 try - catch -finally
周围):
this.EventFiringEnabled = false;
// Do the changes
item.Update();
this.EventFiringEnabled = true;
另一种情况更强硬,以及我使用的原因 ItemUpdated
相反,如果可能的话!
你可以检查 BeforeProperties
, ListItem
和 AfterProperties
在你的 ItemUpdating
事件接收器检查是否有更新正在进行,你必须采取行动。如果没有,那么就 return
.
这是一个指南 当你可以使用 .BeforeProperties
当你该进去的时候。 .ListItem
相反,
其他提示
public override void ItemUpdating(SPItemEventProperties properties)
{
try
{
//is the item checked out?
if (isCheckin(properties) == false)
{
this.EventFiringEnabled = false;
//do your stuff
}
}
catch(Exception a)
{
//catch any errors
}
finally
{
this.EventFiringEnabled = true;
}
}
private bool isCheckin(SPItemEventProperties properties)
{
if (properties.AfterProperties["vti_sourcecontrolcheckedoutby"] == null
&& properties.BeforeProperties["vti_sourcecontrolcheckedoutby"] != null)
{
return true;
}
return false;
}
.
@Robert关于之前的属性,是的,你的权利是不管......只有我可以想到的可能解决方案,而不是
private bool isCheckin(SPItemEventProperties properties)
{
string BeforeVal = properties.ListItem["vti_sourcecontrolcheckedoutby"];
if (properties.AfterProperties["vti_sourcecontrolcheckedoutby"] == null
&& BeforeVal != null)
{
return true;
}
return false;
}
.