Upload large files to Sharepoint 365 RRS feed

  • Question

  • I'm using the CSOM to upload files to a Sharepoint 365 site.

    I've logged in succesfully with Claims based authentication using methods found here "http://www.wictorwilen.se/Post/How-to-do-active-authentication-to-Office-365-and-SharePoint-Online.aspx"

    But using SaveBinaryDirect on the ClientContext fails with a 405 due to cookies being attached to request too late.

    Another method of using CSOM to upload files is similar to below. But with SP 365, this limits the file size to about 3 meg.   var newFileFromComputer = new FileCreationInformation
                        Content = fileContents,
                        Url = Path.GetFileName(sourceUrl)

     Microsoft.SharePoint.Client.File uploadedFile = customerFolder.Files.Add(newFileFromComputer);


      Is there ANY way to do this using CSOM, SP 365 and file sizes up to say 100 meg?
    Monday, February 25, 2013 9:56 PM


  • Well, I haven't found a way to do it with the CSOM and that is truly infuriating.

    A work around was posted by SEvans at the comments on http://www.wictorwilen.se/Post/How-to-do-active-authentication-to-Office-365-and-SharePoint-Online.aspx .

    Basically just do an http put and attach the cookie collection from the claims based authentication. SEvans posted workaround is below

    Great piece of code Wichtor. As others have noted, SaveBinaryDirect does not work correctly, as the FedAuth cookies never get attached to the HTTP PUT request that the method generates.

    Here's my workaround - hope this helps some of you:

    // "url" is the full destination path (including filename, i.e. https://mysite.sharepoint.com/Documents/Test.txt) 
    // "cookie" is the CookieContainer generated from Wichtor's code 
    // "data" is the byte array containing the files contents (used a FileStream to load) 
    System.Net.ServicePointManager.Expect100Continue = false; 
    HttpWebRequest request = HttpWebRequest.Create(url) as HttpWebRequest; 
    request.Method = "PUT"; 
    request.Accept = "*/*"; 
    request.ContentType = "multipart/form-data; charset=utf-8"; 
    request.CookieContainer = cookie; request.AllowAutoRedirect = false; 
    request.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)"; 
    request.Headers.Add("Accept-Language", "en-us"); 
    request.Headers.Add("Translate", "F"); request.Headers.Add("Cache-Control", "no-cache"); request.ContentLength = data.Length; 
    using (Stream req = request.GetRequestStream()) 
    { req.Write(data, 0, data.Length); } 
    HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 
    Stream res = response.GetResponseStream(); 
    StreamReader rdr = new StreamReader(res); 
    string rawResponse = rdr.ReadToEnd(); 
    • Marked as answer by Qiao Wei Thursday, March 7, 2013 10:05 AM
    Wednesday, February 27, 2013 2:37 PM