none
Uploading bigger files using CSOM

    Question

  • Hi,

    whenever i try to use this code

    var myFile = new FileCreationInformation();
    myFile.ContentStream = doc.ContentStream;
                
                using (ClientContext ctx = TokenHelper.GetClientContextWithAccessToken((string)request.Properties[hW], (string)request.Properties[aT]))
                {
                    Folder destionationFolder = ctx.Web.GetFolderByServerRelativeUrl(destinationPath);
                    ctx.Load(destionationFolder);
                    ctx.ExecuteQuery();
    
                    File uploadedFile = destionationFolder.Files.Add(myFile);
                    ctx.Load(uploadedFile);
                    ctx.ExecuteQuery();
                }

     I get the following error message:

    "Specified argument was out of the range of valid values.\r\nParameter name: bytesToCopy"

    Maybe there is another way to upload a file which is bigger than 2MB using the CSOM in combination with OAuth (SharePoint 2013/Office365?

    best regards

    Joerg


    Thursday, December 20, 2012 12:25 PM

Answers

  • I found a solution - it was a Problem with the MemoryStream:

                   var myFile = new FileCreationInformation();
                   using (var stream = content)
                    {
                        stream.Seek(0, SeekOrigin.Begin); <-- The missing statement
                        myFile.ContentStream = stream;
                        myFile.Url                   =  FileName;
                        myFile.Overwrite         = true;

                        using (ctx)
                        {
                            Folder destionationFolder = ctx.Web.GetFolderByServerRelativeUrl(options.SPDestinationPath);
                            ctx.Load(destionationFolder);
                            ctx.ExecuteQuery();
                            File uploadedFile = destionationFolder.Files.Add(myFile);

                            ctx.Load(uploadedFile);
                            ctx.ExecuteQuery();
                        }
                    }


    Monday, February 18, 2013 5:09 PM

All replies

  • Maybe there is another way to upload files greater than 3 MB to Office 365 instances using the client object model?

    Can somebody help me please?

    regards

    Joerg

    Sunday, December 23, 2012 3:49 PM
  • Hi, You can try:

            public static void UploadFile(SP.ClientContext context, SP.Folder folder, string name)
            {
                using (FileStream fileStream = new FileStream("C:\\NewDocument2.docx", FileMode.Open))
                {
                    SP.FileCreationInformation flciNewFile = new SP.FileCreationInformation();
    
                    flciNewFile.ContentStream = fileStream;
                    flciNewFile.Url = name;
                    flciNewFile.Overwrite = true;
    
                    SP.File uploadFile = folder.Files.Add(flciNewFile);
    
                    context.Load(uploadFile);
                    context.ExecuteQuery();    
                }
            }

    Friday, February 08, 2013 1:10 PM
  • Normally you can circumvent the 3MB limit by setting the MaxReceivedMessage size property that is described in http://gallery.technet.microsoft.com/The-Migration-Dragon-for-628acae0 That piece of code uses the server object model and you're not allowed to change this setting in Office 365. On premises, in 2013, you could use that. I built the tool for 2010, but here's the piece of sample code:

    private void IncreaseMaxReceivedMessageSize()
     {
       int increaseSize = Convert.ToInt32(txtBatchSize.Text) * 1024 * 1024;
      
      SPWebService contentService = SPWebService.ContentService;    
      contentService.ClientRequestServiceSettings.MaxReceivedMessageSize = increaseSize;
      contentService.Update();  
    }
     


    Kind regards,
    Margriet Bruggeman

    Lois & Clark IT Services
    web site: http://www.loisandclark.eu
    blog: http://www.sharepointdragons.com

    Friday, February 08, 2013 1:25 PM
  • You can use SaveBinaryDirect instead of file.add("File");

    using (ClientContext clientContext = TokenHelper.GetClientContextWithAccessToken((string)request.Properties[hW], (string)request.Properties[aT]))
                {
                    
                    Web web = clientContext.Web;
                    
                    List documentsList = null;

                    
                        documentsList = clientContext.Web.Lists.GetByTitle(pstrListTitle);
                    

                    clientContext.Load(web, website => website.ServerRelativeUrl);
                    clientContext.Load(documentsList, DocList => DocList.ForceCheckout);
                    clientContext.ExecuteQuery();

                 

                    var fileCreationInformation = new FileCreationInformation();

                    // Assign to content byte[] i.e. documentStream
                    fileCreationInformation.Content = System.IO.File.ReadAllBytes(filePath);

                    // Allow owerwrite of document
                    fileCreationInformation.Overwrite = true;

                    // Upload URL
                    fileCreationInformation.Url = fileURL;
                    
                    using (FileStream fs = new FileStream(filePath, FileMode.Open))
                    {
                        Microsoft.SharePoint.Client.File.SaveBinaryDirect(clientContext, strServerRelativeURL, fs, true);
                    }

                    clientContext.ExecuteQuery();
                }

    Wednesday, February 13, 2013 8:05 AM
  • I found a solution - it was a Problem with the MemoryStream:

                   var myFile = new FileCreationInformation();
                   using (var stream = content)
                    {
                        stream.Seek(0, SeekOrigin.Begin); <-- The missing statement
                        myFile.ContentStream = stream;
                        myFile.Url                   =  FileName;
                        myFile.Overwrite         = true;

                        using (ctx)
                        {
                            Folder destionationFolder = ctx.Web.GetFolderByServerRelativeUrl(options.SPDestinationPath);
                            ctx.Load(destionationFolder);
                            ctx.ExecuteQuery();
                            File uploadedFile = destionationFolder.Files.Add(myFile);

                            ctx.Load(uploadedFile);
                            ctx.ExecuteQuery();
                        }
                    }


    Monday, February 18, 2013 5:09 PM
  • SaveBinaryDirect() does not work with Office 365 / SPO.

    It uses HTTP DAV and does not support Claims Authentication.

    Wednesday, July 09, 2014 12:20 PM