Provider Hosted App – Upload Large Files using CSOM

We all at some time struggled with uploading large files using CSOM in O365. Using managed client object model, we can upload a maximum of 2MB file size. If the file is of larger size, we usually had two options:

  1. File.SaveBinaryDirect
  2. Using REST which supports upto 2GB

The first option can not be used in Sharepoint Online because SaveBinaryDirect does not work with claims authentication (check this link). Does that mean we are left with REST only? Actually NO. We have one more way by which we can upload files greater than 2MB in O365 using CSOM in provider hosted app. But let’s first see the standard CSOM way which allows us 2MB file upload.

Now the method to upload larger files is exactly the same. We only have to use FileCreationInformation.ContentStream property instead of FileCreationInformation.Content property. The whole code is present below:

In some cases, you might get a FileNotFoundException while using above code. Below is another way to use the code

Note that I replaced FileStream with the input stream present in the uploaded file itself.

P.S. I have tested the above code for files upto 50 MB. It depends largely on internet speed. Once for 50MB file size it gave me time out error (which could be fixed by setting RequestTimeout property of client context)

Hope this helps.

Reference:

  1. O365 AMS
Advertisements

11 thoughts on “Provider Hosted App – Upload Large Files using CSOM

  1. Pingback: SharePoint 2013: Recopilatorio de enlaces interesantes (XXXIV)! - Blog de Juan Carlos González en Geeks.MS

  2. Hey Thank you for the blog!! I have a quick question. I tried uploading the file with size > 1.5MB using the second approach as mentioned by you. But the code failes when it is deployed in the map the error message says that “Could not find a part of the file”. Though the code works perfectly in the local app testing from VS 2012.

    • Please make sure that you are using the same file.

      Besides that I have added another version of the code above. In that I replaced FileStream with the input file inbuilt stream property. Please try that.

      • Input stream works with out any error. However the uploaded file will have zero bytes i.e. a blank file gets uploaded with input stream.

        BTW earlier approach works fine during debug in VS 2012. But when the app is deployed in the server it fails not sure what might be the reason.

  3. Pingback: Provider Hosted App – Copy Document across Site Collections | My SharePoint Learnings

  4. Where is the code in this article …. I cant find any code here , i’m in the same requirement .. i tried with the CSOM but im getting access denied error while reading the file from FileUpload Control , so please help me in this.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s