3

I have created a document library and I have applied an Item Added event receiver to that library which will copy that file to another library and I have added that library as a mapped drive to my local machine.

Now what happens is that when I am adding file to that map drive my event receiver is fired(File is still uploading).

So I want to wait until file is copied. Is it possible? if yes then How?

2
  • Ok, I know this is answered. But, just asking, you have Doc lib 'A' with item added event receiver. This ER copies the file to doc lib 'B'. The doc Lib 'B' is mapped to local machine. When you say you are adding a file, initially where are you adding the file? In doc lib A, doc lib B, or in the mapped drive? confused.
    – variable
    Jun 4, 2014 at 12:59
  • I am adding document to doc lib A.
    – Torque
    Jun 4, 2014 at 13:21

1 Answer 1

8

As far as I understand your question, i think there is problem when your are uploading file through a mapped drive. If you copy & paste a file over windows explorer (WebDAV) to the Library the EventHandle (ItemAdded) will trigger as soon the File was created. But this doesn't mean the File is already filled with data. So one workaround should be to put some delay before copying that file, but that would not be a proper solution.

Other workaround should be like using the code like below:

 if (SPContext.Current == null)
        {
          //If the initial file length is 0, pause the thread for 2 seconds
          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;

              }
    }
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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