1

I have a document library in which I have attached two event receivers.

  1. Item Added.
  2. Item Updated.

My event receiver definition is:

<Receiver>
    <Name>ReceiverItemAdded</Name>
    <Type>ItemAdded</Type>
    <Assembly>XXX</Assembly>
    <Class>XXX</Class>
    <SequenceNumber>10000</SequenceNumber>
    <Synchronization>Synchronous</Synchronization>
</Receiver>

The event receivers simply copies the item and metadata to other document library.

The event receivers work fine if I upload the document from SharePoint. But the Item Added event receiver does not work if I upload multiple files using explorer. The destination file shows the 0 KB size, but the metadata is copied successfully.

The code which copies the file is as below:

// Copy File                            
SPFile newFile = destLibrary.RootFolder.Files.Add(addedItem.File.Name, addedItem.File.OpenBinary());
SPListItem newItem = newFile.Item;
newItem[FieldNames.ImagesId] = addedItem[SPBuiltInFieldId.ID];
newItem.Update();

It is must to solve the issue as users would use the explorer to upload the file. Where is the point that to be caught in the code which will trigger Item Added event receiver if file is uploaded using explorer.

Please help.

  • You you event is firing but its not copying the data to another library? Can you please elaborate how are you copying the file to another library? – Aanchal Jun 3 '14 at 14:56
  • Hello sir, I have updated my question and added the code which copies the file.@Aanchal – users1100 Jun 4 '14 at 4:24
  • SO your file copies to your destination folder? – Aanchal Jun 4 '14 at 4:27
  • The new item is created at the destination library but the file does not appear i.e. file size becomes 0 KB – users1100 Jun 4 '14 at 4:50
  • Is there any idea regarding the issue?@Aanchal – users1100 Jun 4 '14 at 6:30
2

You problem is similar to the following issue : https://stackoverflow.com/questions/6528623/sharepoint-itemadded-and-spfile-openbinary-zero-bytes

So you can just try to add some delay before uploading the file. This is not a solution for your problem, but can be a workaround for your solution.

You can try following check in your code:

if (properties.ListItem.File.Length == 0)
             {

               System.Threading.Thread.Sleep(2000);

               //Since our item exists, run the GetItemById to instantiate a new  and updated SPListItem object 
               var spFile = properties.List.GetItemById(properties.ListItemId);

               //SharePoint places an Exclusive lock on the file while the data is being loaded into the file
               while (spFile.File.LockType != SPFile.SPLockType.None)
                     {
                       System.Threading.Thread.Sleep(2000);
                       spFile = properties.List.GetItemById(properties.ListItemId);

                       //We need to check if the file exists, otherwise it will loop forever if someone decides to cancel the upload
                       if (!spFile.File.Exists)
                        return;
                     }

                //If someone thought it was a good idea to actually load a 0 byte file, don't do anything else
                if (spFile.File.Length == 0)
                       return;

              }
  • Thanks for replying sir, I read this post in stack but the users who are uploading the documents has average length of document is 30-50 MB. – users1100 Jun 4 '14 at 12:28
  • I got something on internet that when it takes to upload file more than delay time in thread also causes issue. – users1100 Jun 4 '14 at 12:28
  • I have updated the answer, you can add another check also. This is only a workaround, You can test it to check if it works at your end. – Aanchal Jun 4 '14 at 12:36

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.