Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

SharePoint 2010: How to upload a file of the size of 1 GB using client object model


Symptoms

When you use SharePoint Client OM to upload files to SharePoint 2010 Document Library, uploading of files larger than 3 MB fail with the following error:

The remote server returned an error: (400) Bad Request.

Stack trace:

System.Net.WebException was unhandled
 Message=The remote server returned an error: (400) Bad Request.
 Source=System
 StackTrace:
      at System.Net.HttpWebRequest.GetResponse()
      at Microsoft.SharePoint.Client.SPWebRequestExecutor.Execute()
      at Microsoft.SharePoint.Client.ClientRequest.ExecuteQueryToServer(ChunkStringBuilder sb)
      at Microsoft.SharePoint.Client.ClientRequest.ExecuteQuery()
      at Microsoft.SharePoint.Client.ClientRuntimeContext.ExecuteQuery()
      at Microsoft.SharePoint.Client.ClientContext.ExecuteQuery()
      at SPClientOMConsoleApp.Program.Main(String[] args) in C:\SPClientOMConsoleApp\SPClientOMConsoleApp\Program.cs:line 32
      at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
      at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
      at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
      at System.Threading.ThreadHelper.ThreadStart()
 InnerException:

Sample program used to upload file using SharePoint 2010 Client OM:

            ClientContext clientContext = new ClientContext("http://server:10000/sites/testSite");
            Web web = clientContext.Web;
            clientContext.Load(web);
            List docLib = web.Lists.GetByTitle("doclib");
            clientContext.Load(docLib);
            clientContext.ExecuteQuery();
            byte[] bFile = System.IO.File.ReadAllBytes(@"C:\largeFile.docx");
            FileCreationInformation file = new FileCreationInformation();
            file.Content = bFile;
            file.Url = "http://server:10000/sites/testSite/doclib/" + "test.docx";
            Microsoft.SharePoint.Client.File newFile = docLib.RootFolder.Files.Add(file);
            clientContext.Load(newFile);
            clientContext.ExecuteQuery();

Requirement:

How to upload files with larger size, for example up to 1 GB or more, using Client Object Model in SharePoint 2010.

↑ Back to the top


Cause

The default internal restrictions on file sizes and timeout settings halt uploading of larger files using Client Object Model in SharePoint.

↑ Back to the top


Resolution

Let's consider that we need to upload a file that is close to the maximum upload size allowed by the SharePoint infrastructure. In order to achieve this, we first need to change the default values related to the file upload process i.e. the maximum allowed file size in SharePoint 2010, the maximum received message size in the Client request service settings for SharePoint 2010 and the timeout settings specified in the web.config of the SharePoint web application. Please find the details below:

Increase the Maximum allowed file size to upload to SharePoint 2010 Libraries using the below steps:

  1. Browse to SharePoint 2010 Central Administration site -> Application Management -> Manage Web Applications (<CentralAdminURL>/_admin/WebApplicationList.aspx) -> Select the Web Application where you want to upload large files -> Select General Settings from the Ribbon menu.
  2. Once on the "Web Application General Settings" page, scroll down to locate the "Maximum Upload Size" section.
  3. Change the value for "Maximum upload size" to 2047 MB (2 GB is the maximum allowed value to be set in this section)
  4. Click OK to confirm and save the setting changes.

Increase the timeout settings in the web.config file of the web application:

  1. On the SharePoint 2010 server machine, browse to the folder "C:\inetpub\wwwroot\wss\VirtualDirectories" and select the source folder related to the above web application.
  2. In this folder, you should find the applications web.config file.
  3. Open the web.config file in a text editor like Notepad and locate the HTTPRuntime element (there will be multiple HTTPRuntime elements in this file to make changes to).
  4. Now set the maxRequestLength = "2097151" and executionTimeout = "1800000"
  5. Also locate the jsonSerialization element and set the value to  "2146435072"
  6. Save and close the web.config file.

Increase the Client request service settings for SharePoint 2010:

Run the below code in a console application to increase the value for this setting:

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using System.IO;

namespace SPClientOMConsoleApp

{
    class Program
    {
        static void Main(string[] args)
        {            
            SPWebService ws = SPWebService.ContentService;

            SPClientRequestServiceSettings clientSettings = ws.ClientRequestServiceSettings;

            clientSettings.MaxReceivedMessageSize = 2146435072;

            ws.Update();

            Console.WriteLine(clientSettings.MaxReceivedMessageSize);

        }
    }
}

Now that we have increased the setting values, we can use the two code options specified below to upload the larger files using SharePoint 2010 Client Object Model:

Option 1:

As mentioned above, use the Microsoft.SharePoint.Administration.SPClientRequestServiceSettings API to set the MaxReceivedMessageSize to higher values, for example  2 GB. Note that the MaxReceivedMessageSize property value needs to be set only once and does not need to be run every time the client OM based file upload code is triggered:

(To ensure that this option works set the project properties to use .NET Framework 3.5 and target platform as “Any CPU”)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Client;
using System.IO;

namespace SPClientOMConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {      
            ClientContext clientContext = new ClientContext("http://server:10000/sites/testSite");
            Web web = clientContext.Web;
            clientContext.Load(web);
            List docLib = web.Lists.GetByTitle("doclib");
            clientContext.Load(docLib);
            clientContext.ExecuteQuery(); 

            byte[] bFile = System.IO.File.ReadAllBytes(@"C:\largeFile.docx");
            FileCreationInformation file = new FileCreationInformation();
            file.Content = bFile;
            file.Url = "http://server:10000/sites/testSite/doclib/" + "test.docx";
            Microsoft.SharePoint.Client.File newFile = docLib.RootFolder.Files.Add(file);
            clientContext.Load(newFile);
            clientContext.ExecuteQuery();
        }
    }
}

Option 2:

Using HTTP DAV we send the raw binary data across the wire and do not need to increase the received message size.  This is the preferred upload method when using managed client object model. 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Client;
using System.IO;

namespace SPClientOMConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            ClientContext context = new ClientContext("http://server:10000/sites/testSite");
            using (FileStream fs = new FileStream(@"C:\largeFile.docx", FileMode.Open))
            {
                Microsoft.SharePoint.Client.File.SaveBinaryDirect(context, "/sites/testSite/doclib/test.docx", fs, true);
            }
        }
    }
}

Both the above options have been tested for a successful upload of a file of sizes up to 1 GB. For larger values (say a file of size 1.8 GB), use option 2 (HTTP DAV) shown above.

↑ Back to the top


More Information

At times you may see that the upload operation returns a timeout exception but this is critical only for very large file sizes which are close to 2 GB. Otherwise, it can be ignored using a try-catch block and you should see that the file got uploaded successfully.

↑ Back to the top


Keywords: vkball, kb

↑ Back to the top

Article Info
Article ID : 2529243
Revision : 2
Created on : 4/26/2018
Published on : 4/26/2018
Exists online : False
Views : 141