複数回発生するItemUpdatingイベントレシーバーの処理
-
29-09-2020 - |
質問
ドキュメント ライブラリの場合にアイテム更新イベント レシーバーが 2 回起動する理由は、チェックイン/チェックアウトのためです。
また、この項目更新イベントで現在の項目値を更新している場合、ドキュメント ライブラリ/リストの場合は 2 回起動する可能性があります。
これら両方のケースをコード経由で処理するにはどうすればよいでしょうか?
解決
あなたが 2 番目のトリガーを行うシナリオの場合 ItemUpdating
項目の値を変更すると、 EventFiringEnabled
パラメータ ( try - catch -finally
その周りに):
this.EventFiringEnabled = false;
// Do the changes
item.Update();
this.EventFiringEnabled = true;
もう 1 つのシナリオはより困難であり、私がこのシナリオを使用する背後にある理由 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;
}
. 所属していません sharepoint.stackexchange