How to upload document to SharePoint document library using CSOM

This SharePoint CSOM tutorial explains, how to a document to the SharePoint document library using CSOM (client-side object model) from the physical location.

We will also see how to fix error, Unable to read data from the transport connection An existing connection was forcibly closed by the remote host.

We can upload a document to a SharePoint document library using the SharePoint server-side object model or SharePoint Web Services and also we can upload a document using PowerShell also. Many times there is a requirement to upload a document from a physical path to document library using Client Side Object Model (csom) in SharePoint 2013.

upload document to SharePoint document library programmatically

Upload document to SharePoint document library using CSOM Programmatically

Below, is the CSOM code to upload a document to the SharePoint document library programmatically using csom in SharePoint 2013/2016.

You can write the code inside the console application or windows application in c#.net.

static void Main(string[] args)
{

try
{
// Starting with ClientContext, the constructor requires a URL to the server running SharePoint.
using(ClientContext client = newClientContext("http://servername146/sites/test/"))
{
//client.Credentials = System.Net.CredentialCache.DefaultCredentials;

// Assume that the web site has a library named "FormLibrary".
var formLib = client.Web.Lists.GetByTitle("Documents");
client.Load(formLib.RootFolder);
client.ExecuteQuery();

// FormTemplate path, The path should be on the local machine/server !
string fileName = @ "C:\File.txt";

var fileUrl = "";

//Craete FormTemplate and save in the library.
using(var fs = newFileStream(fileName, FileMode.Open))
{
var fi = newFileInfo("test.txt"); //file Title
fileUrl = String.Format("{0}/{1}", formLib.RootFolder.ServerRelativeUrl, fi.Name);
Microsoft.SharePoint.Client.File.SaveBinaryDirect(client, fileUrl, fs, true);
client.ExecuteQuery();
}

// Get library columns collection.
var libFields = formLib.Fields;
client.Load(libFields);
client.ExecuteQuery();

Microsoft.SharePoint.Client.File newFile = client.Web.GetFileByServerRelativeUrl(fileUrl);

ListItem item = newFile.ListItemAllFields;

item["Title"] = "test";
item.Update();
client.ExecuteQuery();
}

} catch (Exception ex)
{

}

}

Final Output:

upload document to SharePoint document library programmatically using CSOM

Unable to read data from the transport connection An existing connection was forcibly closed by the remote host

Now, we will see, how fix the below issue which usually comes when we do some bulk operation in SharePoint Online.

Recently we were doing some bulk operation from a console application to a SharePoint online site. But we were getting the below error:

An exception of type ‘System.IO.IOException’ occurred in Microsoft.SharePoint.Client.Runtime.dll but was not handled in user code.

Additional information: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.

An existing connection was forcibly closed by the remote host

In SharePoint Online, we were trying to upload multiple files to a SharePoint document library. We were trying to upload files inside a single button click, but then we have divided and uploaded into multiple button clicks. Then it worked correctly.

You may like following SharePoint tutorials:

The physical document is uploaded to the SharePoint document library. Here we discussed how to upload documents to document library using csom in SharePoint 2013/2016.

Donwload Hub site pdf

Download SharePoint Online Tutorial PDF FREE!

Get update on Webinars, video tutorials, training courses etc.

>