none
SaveBinaryDirect throw 404 error when uploading .cs file.

    Question

  • Hi all,

    I don't know why this happened. The server always give me 404 error when uploading .cs file using SaveBinaryDirect. If I rename the extension to others, it can be uploaded successfully. Even if there's any particular settings on the server side, the response should not be 404...

    Any clue?

    Monday, March 12, 2012 12:20 PM

Answers

  • Check this article http://technet.microsoft.com/en-us/library/cc262496.aspx. Maybe .cs is in blocked file types on your server.

    Oleg

    • Marked as answer by Shimin Huang Friday, March 23, 2012 6:15 AM
    Monday, March 12, 2012 12:31 PM
  • Looks like a bug. I could reproduce this issue.

    This code throw the exception 404:

    clientContext = new ClientContext(@"http://myserver/sites/test/");
    using (FileStream fs = new FileStream(@"C:\Temp\test.cs", FileMode.Open))    
    {
        Microsoft.SharePoint.Client.File.SaveBinaryDirect(clientContext, "/documents/test.cs", fs, true);   
    }

    This code uploads .CS file without exception:

    ClientContext context = new ClientContext("http://myserver/sites/test/");
    Web web = context.Web;
    FileCreationInformation newFile = new FileCreationInformation();
    newFile.Content = System.IO.File.ReadAllBytes(@"C:\Temp\test.cs");
    newFile.Url = "test.cs";
    List docs = web.Lists.GetByTitle("Documents");
    Microsoft.SharePoint.Client.File uploadFile = docs.RootFolder.Files.Add(newFile);
    context.Load(uploadFile);
    context.ExecuteQuery();
    My suggestion would be to use secont example instead SaveBinaryDirect.

    Oleg

    • Marked as answer by Shimin Huang Friday, March 23, 2012 6:15 AM
    Monday, March 12, 2012 5:31 PM
  • You can upload a file via UI but you will not able to open or save .CS file from document library. I think this is related to ASP.NET
    ValidateRequest attribute. "Request validation checks all input data against a hard-coded list of potentially dangerous values". Read this http://msdn.microsoft.com/en-us/library/ydy4x04a.aspx and this http://msdn.microsoft.com/en-us/library/system.web.httprequestvalidationexception.aspx.

    Oleg

    • Marked as answer by Shimin Huang Friday, March 23, 2012 6:15 AM
    Monday, March 12, 2012 6:26 PM
  • Read my previous post. SharePoint is not blocking the .CS files. The issue is in ASP.NET security settings. You can disable this security check but I would not suggest doing that.

    Oleg

    • Marked as answer by Shimin Huang Friday, March 23, 2012 6:15 AM
    Friday, March 16, 2012 12:34 PM

All replies

  • Check this article http://technet.microsoft.com/en-us/library/cc262496.aspx. Maybe .cs is in blocked file types on your server.

    Oleg

    • Marked as answer by Shimin Huang Friday, March 23, 2012 6:15 AM
    Monday, March 12, 2012 12:31 PM
  • Thanks Oleg.

    But I can upload .cs file successfully via browser.

    Monday, March 12, 2012 2:11 PM
  • Looks like a bug. I could reproduce this issue.

    This code throw the exception 404:

    clientContext = new ClientContext(@"http://myserver/sites/test/");
    using (FileStream fs = new FileStream(@"C:\Temp\test.cs", FileMode.Open))    
    {
        Microsoft.SharePoint.Client.File.SaveBinaryDirect(clientContext, "/documents/test.cs", fs, true);   
    }

    This code uploads .CS file without exception:

    ClientContext context = new ClientContext("http://myserver/sites/test/");
    Web web = context.Web;
    FileCreationInformation newFile = new FileCreationInformation();
    newFile.Content = System.IO.File.ReadAllBytes(@"C:\Temp\test.cs");
    newFile.Url = "test.cs";
    List docs = web.Lists.GetByTitle("Documents");
    Microsoft.SharePoint.Client.File uploadFile = docs.RootFolder.Files.Add(newFile);
    context.Load(uploadFile);
    context.ExecuteQuery();
    My suggestion would be to use secont example instead SaveBinaryDirect.

    Oleg

    • Marked as answer by Shimin Huang Friday, March 23, 2012 6:15 AM
    Monday, March 12, 2012 5:31 PM
  • You can upload a file via UI but you will not able to open or save .CS file from document library. I think this is related to ASP.NET
    ValidateRequest attribute. "Request validation checks all input data against a hard-coded list of potentially dangerous values". Read this http://msdn.microsoft.com/en-us/library/ydy4x04a.aspx and this http://msdn.microsoft.com/en-us/library/system.web.httprequestvalidationexception.aspx.

    Oleg

    • Marked as answer by Shimin Huang Friday, March 23, 2012 6:15 AM
    Monday, March 12, 2012 6:26 PM
  • Hi Oleg,

    It's weired the server gave me an 404 error if it's a validation failure. And also ".cs" file is not listed in this list:

    http://technet.microsoft.com/en-us/library/cc262496.aspx

    I don't know if there's any other file type has the same problem so I have to wait someone (hopefully MSFTs) give me an officially list of file types can cause this problem.

    Friday, March 16, 2012 3:22 AM
  • Read my previous post. SharePoint is not blocking the .CS files. The issue is in ASP.NET security settings. You can disable this security check but I would not suggest doing that.

    Oleg

    • Marked as answer by Shimin Huang Friday, March 23, 2012 6:15 AM
    Friday, March 16, 2012 12:34 PM