ItemUpdating called twice after ItemAdded in event receiver

sharepoint-2010

I've created an event receiver to handle the ItemAdded and ItemUpdating events on a document library in SharePoint 2010.

I've encountered a problem where when I add a document to the library (e.g. by saving it back from Word) the ItemAdded method is correctly called however this is then followed by two calls to ItemUpdating. I have removed all code from my handlers to ensure that it's not something I'm doing inside that is causing the problem. They literally look like:

public override void ItemUpdating(SPItemEventProperties properties)
{
}

public override void ItemAdded(SPItemEventProperties properties)
{
}

Does anyone have a solution to this issue?

Here is my elements.xml file for the event receiver:

<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Receivers ListTemplateId="101">
      <Receiver>
        <Name>DocumentsEventReceiverItemUpdating</Name>
        <Type>ItemUpdating</Type>
        <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
        <Class>My.Namespace.DocumentsEventReceiver</Class>
        <SequenceNumber>10000</SequenceNumber>
        <Synchronization>Synchronous</Synchronization>
      </Receiver>
      <Receiver>
        <Name>DocumentsEventReceiverItemAdded</Name>
        <Type>ItemAdded</Type>
        <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
        <Class>My.Namespace.DocumentsEventReceiver</Class>
        <SequenceNumber>10000</SequenceNumber>
        <Synchronization>Synchronous</Synchronization>
      </Receiver>
  </Receivers>
</Elements>

Best Answer

Problem is that In Document library event handlers during Item Updating also checking that Document is it in Check In mode or Check Out. That's why it is called twice.

You should put your code in

 public override void ItemUpdating(SPItemEventProperties properties)
  {  
    base.ItemUpdating(properties);
    if (properties.AfterProperties["vti_sourcecontrolcheckedoutby"] == null && properties.BeforeProperties["vti_sourcecontrolcheckedoutby"] != null)
     {
       //do stuff
     }
  }

For further details Here is good article for describe whole situation of the Document's Events.

Related Topic