I'm trying to duplicate a file from one library to another within a Remote Event Receiver.

When reaching the UploadFile-function, I get the following error:

Specified argument was out of the range of valid values. Parameter name: bytesToCopy

Information about the function can be found here. It has the following signature:

public static Microsoft.SharePoint.Client.File UploadFile (this Microsoft.SharePoint.Client.Folder folder, string fileName, System.IO.Stream stream, bool overwriteIfExists);

My code is:

using (System.IO.MemoryStream stream = new System.IO.MemoryStream())
    ClientResult<Stream> data = curItem.File.OpenBinaryStream();

    if (data != null && data.Value != null)

         UploadedFile = destinationList.RootFolder.UploadFile(curItem.File.Name.ToString(), stream, true);

As a note: curItem, destinationList and curItem.File are all loaded in the context.

How to get rid of the error?

1 Answer 1


Someone over at StackOverflow answered the question:

Courtesy of Oerkelens:

A quick google search lead me to this article

Someone had a similar problem (although he figured it had to do with file size) and solved it by resetting the stream before sending it to SharePoint:

using (var stream = content)
    stream.Seek(0, SeekOrigin.Begin); // <-- The missing statement

Hope that works for you as well :)

Your Answer

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

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