Wednesday, 3 June 2009

Check out causing the item update event receivers call twice in SharePoint

Event receivers will help us to write events to add custom logic to perform operations while adding/updating/deleting items in a list/libraries in SharePoint. We can register the receivers to all lists/libraries or to a particular list or a library. Here in this post I am explaining the special case of the item update event receivers.

This is regarding the calling of these events 2 times by SharePoint framework when check out action done on a document library item. When you enable the option Require Check Out in a document library and when you choose option to checkout a document in a document library then it will call the item update events twice [If you write any update event receivers on the list then only]. So take care to avoid the unwanted call, otherwise it will do some unwanted actions. Because of it there may be chances of performance issue. Below are the details of solving the issue.

If the vti_sourcecontrolcheckedoutby property exits in the "BeforeProperties" property but not in the "AfterProperties" property, the event was caused by checking in a document. So by using this logic we will put the logic in specific block.

if (properties.AfterProperties["vti_sourcecontrolcheckedoutby"] == null
&& properties.BeforeProperties["vti_sourcecontrolcheckedoutby"] != null)
{
    //This is when the update event is triggered by check-in.
}
else
{
    //This is triggered by events other than check-in action.
}

For more details:

http://support.microsoft.com/kb/939307

No comments:

Post a Comment