2

In a SharePoint 2013 document library, on uploading a document the event receivers are fired in the following order ItemAdding --> ItemAdded. Likewise when re-uploading the same document ItemUpdating --> ItemUpdated are fired.

The metadata of the item is updated in the ItemAdded and ItemUpdated event respectively. But the Edit form dialog which pops open after document upload sometimes (intermittently) shows the old metadata (prior to ItemAdded/ItemUpdated). If the Edit form is closed without clicking on Save we can see the correct updated data in the document library. This indicated the Edit form opens up before the document metadata is updated in the ItemAdded/ItemUpdated event receiver.

Possible solutions already tried :

  • Optimized the code to execute ItemAdded/ItemUpdated (decreased the occurrence of issue but did not solve).

  • Added Synchronous tag for the ItemAdded and the ItemUpdated event receivers (no effect on the issue).

  • Using properties.AfterProperties[""] to update values in ItemAdding and ItemUpdating but the values which need to be nulled are not reflected.

Any other suggestions to resolve this intermittent issue?

Edit 1 -

We have registered the event receiver in a declarative manner -

<Receiver> 
   <Name>MyEventReceiversItemAdded</Name> 
   <Type>ItemAdded</Type> 
   <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly> 
   <Class>My.ModelLibraryEventReceivers</Class> 
   <SequenceNumber>10000</SequenceNumber> 
   <Synchronization>Synchronous</Synchronization> 
</Receiver>

Edit 2 -

Elements.xml file

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Receivers ListTemplateId="10001">
      <Receiver>
        <Name>ModelLibraryEventReceiversItemAdding</Name>
        <Type>ItemAdding</Type>
        <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
        <Class>My.ModelLibraryEventReceivers</Class>
        <SequenceNumber>10000</SequenceNumber>
      </Receiver>
      <Receiver>
        <Name>ModelLibraryEventReceiversItemUpdating</Name>
        <Type>ItemUpdating</Type>
        <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
        <Class>My.ModelLibraryEventReceivers</Class>
        <SequenceNumber>10000</SequenceNumber>
      </Receiver>
      <Receiver>
        <Name>ModelLibraryEventReceiversItemDeleting</Name>
        <Type>ItemDeleting</Type>
        <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
        <Class>My.ModelLibraryEventReceivers</Class>
        <SequenceNumber>10000</SequenceNumber>
      </Receiver>
      <Receiver>
        <Name>ModelLibraryEventReceiversItemCheckingIn</Name>
        <Type>ItemCheckingIn</Type>
        <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
        <Class>My.ModelLibraryEventReceivers</Class>
        <SequenceNumber>10000</SequenceNumber>
      </Receiver>
      <Receiver>
        <Name>ModelLibraryEventReceiversItemCheckingOut</Name>
        <Type>ItemCheckingOut</Type>
        <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
        <Class>My.ModelLibraryEventReceivers</Class>
        <SequenceNumber>10000</SequenceNumber>
      </Receiver>
      <Receiver>
        <Name>ModelLibraryEventReceiversItemAdded</Name>
        <Type>ItemAdded</Type>
        <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
        <Class>My.ModelLibraryEventReceivers</Class>
        <SequenceNumber>10000</SequenceNumber>
        <Synchronization>Synchronous</Synchronization>
      </Receiver>
      <Receiver>
        <Name>ModelLibraryEventReceiversItemUpdated</Name>
        <Type>ItemUpdated</Type>
        <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
        <Class>My.ModelLibraryEventReceivers</Class>
        <SequenceNumber>10000</SequenceNumber>
      </Receiver>
      <Receiver>
        <Name>ModelLibraryEventReceiversItemDeleted</Name>
        <Type>ItemDeleted</Type>
        <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
        <Class>My.ModelLibraryEventReceivers</Class>
        <SequenceNumber>10000</SequenceNumber>
      </Receiver>
      <Receiver>
        <Name>ModelLibraryEventReceiversItemCheckedIn</Name>
        <Type>ItemCheckedIn</Type>
        <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
        <Class>My.ModelLibraryEventReceivers</Class>
        <SequenceNumber>10000</SequenceNumber>
      </Receiver>
      <Receiver>
        <Name>ModelLibraryEventReceiversItemCheckedOut</Name>
        <Type>ItemCheckedOut</Type>
        <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
        <Class>My.ModelLibraryEventReceivers</Class>
        <SequenceNumber>10000</SequenceNumber>
      </Receiver>
  </Receivers>
</Elements>
0

You can declare an ..ed event receiver as synchronous or asynchronous. The behavior you describe definitely happens because your ER is asynchronous. You have to turn it into a synchronous one. This can be easily done at registration time (nothing to change in the code of the ER itself).

How do you register your ER (with XML or with C# code)? I'll explain you how to correctly change the sync/async option.

  • Please let me know the steps to correct change the sync/async option. – xs2mayank Feb 3 '16 at 8:48
  • As I stated, it first depends on the way you register your ER: either declaratively or by code. Please, share your registering artifact with us. – Evariste Feb 3 '16 at 8:51
  • We have registered the event receiver in a declarative manner -<Receiver> <Name>MyEventReceiversItemAdded</Name> <Type>ItemAdded</Type> <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly> <Class>My.ModelLibraryEventReceivers</Class> <SequenceNumber>10000</SequenceNumber> <Synchronization>Synchronous</Synchronization> </Receiver> – xs2mayank Feb 3 '16 at 11:29
  • Can you please update your question with this, for better readibility? Also, add the complete element.xml file (including the parent Receivers node), so we can see how it is registered on lists or CT). Did you deactivate/reactivate the feature after redeploying the new WSP turned to Synchronous? – Evariste Feb 3 '16 at 11:35
  • You element.xml looks good. Try deactivating/reactivating the feature after re-deployment. I don't see any reason this would not work here. This has been done multiple times, and that sync ER stuff usually works (one of the rare things that work well in SP! :)) – Evariste Feb 3 '16 at 14:04
0

I think you need to change the Sequence Number node. They should be in the numerical order of the sequence you wish to follow, for example:

 <Type>ItemAdding</Type>  
 <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly> 
 <Class>My.ModelLibraryEventReceivers</Class> 
 <SequenceNumber>10000</SequenceNumber> 

 <Type>ItemAdded</Type> 
 <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly> 
 <Class>My.ModelLibraryEventReceivers</Class> 
 <SequenceNumber>10001</SequenceNumber>

Hope this helps

EDIT: Reading further into this, the ItemAdded and ItemUpdating events are asynchronous. Meaning they will be queued alongside the loading of the NewForm/EditForm events. As they are both asynchronous events, the new/editform will fetch the version of the data it has to hand (the old version). Try using SystemUpdate() rather than ItemUpdate(). Also this excellent MSDN blog for reference.

  • The order in which event receivers are firing is fine. The problem is that the event receivers some time fires before the Edit Properties windows pops up leading to display of older or null values. – xs2mayank Feb 3 '16 at 13:48
  • As @Evariste says above, have you deactivated/reactivated the feature after redeployment? Having ensured that both of your receivers are synchronous. It would be helpful to see the complete XML – George Grainger Feb 3 '16 at 13:53
  • Yes, the feature has been deactivated/reactivated after deployment. However that had not made any difference. Complete XML has been added in the original question (Edit 2). – xs2mayank Feb 4 '16 at 11:45
0

My suggestion would be to move all of your logic into your -ing synchronous before receivers. All updates to fields go into properties.AfterProperties and reference by InternalName of the SPField. Are you setting them to null or to string.Empty? I think string.Empty works for me.

My event receivers read properties out of office files and update SP columns based on them, including empty values clearing out the SharePoint fields.

If you cannot move the logic out of the -ed event receivers and setting the synchronous flag, ordering, etc does not work, I think your only option is to customize the Edit form to do a 'check and wait' operation. If it is only one library, you could just add a content editor web part on the edit form, extract the current item ID from the query string and do some REST calls every X seconds, query the list and check for the field values that indicate your -ed event completed, and then refresh/show the form once that completes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.