1

I have an event receiver that copies a list item, including the attachments, to another list. Very rarely, the attachment fails to copy over and gives the error "Value does not fall within the expected range". There is no discernible pattern with the attachments that fail--the names and sizes are not unusual and recopying the same attachment works a bit later. It seems like there is some kind of server hiccup that hits at just the right time for this to occur.

The error seems to occur 1% of the time or less. The exact block of code where this is happening is here (there is a try/catch block wrapped around this that is throwing the error):

SPFolder attachmentsFolder = web.Folders["Lists"].SubFolders["List Name"].SubFolders["Attachments"].SubFolders[properties.ListItem.ID.ToString()];
foreach (SPFile file in attachmentsFolder.Files)
{
    byte[] binFile = file.OpenBinary();
    duplicate.Attachments.AddNow(file.Name, binFile);
}

Is there a more reliable way of copying attachments that I am not doing? Has anyone run into this issue before?

  • On which event are you copying the attachments ? – Stefan Bauer Oct 23 '14 at 21:45
  • This is an ItemUpdated event. – user11457 Oct 24 '14 at 15:31
0

You should change this one-liner

web.Folders["Lists"].SubFolders["List Name"].SubFolders["Attachments"].SubFolders[properties.ListItem.ID.ToString()];

to

SPList objList = web.Lists["MyList"];

if(objList != null) {
    // Get the item by ID
    SPListItem objItem = objList.GetItemById(properties.ListItem.ID);

    if(objItem != null) {
        // Get the attachments of the item
        SPAttachmentCollection objAttchments = objItem.Attachments;

        //... code for copying attachements

    }
    else {
         //Log Error
    }
}
else {
    //Log Error
}
  • Okay, I will try retrieving the attachments this way and see if it helps. Since it happens sporadically, it will probably be several weeks until I can tell if there's a difference. – user11457 Oct 24 '14 at 15:35
  • Use proper logging to catch the real issue – Amal Hashim Oct 24 '14 at 15:49
  • I am logging the error to SharePoint's ULS logs; that's how I got the error code. The error simply gives me "Value does not fall within the expected range" in one of these lines. I will see if swapping out attachment collection for spfolder will fix the issue, though. – user11457 Oct 24 '14 at 21:50
0

Try the following code:

private void CopyAttachments(SPListItem sourceItem, SPListItem targetItem)
    {
        try
        {
            //get the folder with the attachments for the source item
            SPFolder sourceItemAttachmentsFolder = sourceItem.Web.Folders["Lists"].SubFolders[List Name].SubFolders["Attachments"].SubFolders[sourceItem.ID.ToString()];

            //Loop over the attachments, and add them to the target item
            foreach (SPFile file in sourceItemAttachmentsFolder.Files)
            {
                byte[] binFile = file.OpenBinary();
                targetItem.Attachments.AddNow(file.Name, binFile);
            }
        }
        catch { }
        finally
        {
            sourceItem.Web.Dispose();
        }
    }
0

If you like to move the attachments you should create ItemAttachemntAdded Event receiver.

 public override void ItemAttachmentAdded(SPItemEventProperties properties)
 {
     //move it in here to another location
 }

I previously tested it with an item update event receiver and fails sometimes the way you did. After I switched the event receiver type it worked perfectly. You will get in every attachment with a single event receiver can so it makes the code pretty slim too.

  • Okay, that's interesting that you had the same problem with it occasionally failing. The problem with using an Attachment event receiver is that I do not always want to copy the item right away--sometimes I have an item that already has an attachment that gets copied later on. – user11457 Oct 27 '14 at 14:19
  • Yeah but you can request it might from the item if it should be copied or not. – Stefan Bauer Oct 27 '14 at 21:09

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.