This SharePoint CSOM tutorial explains, how to upload 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 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:
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.
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:
- Upload documents from network drive to SharePoint 2013/2016 Server document library using PowerShell
- Get URL for uploading document to a document library in SharePoint 2013 online
- Create a Unique Project ID and automated folder structure under document library using Event Receiver in SharePoint 2013/2016/2019
- Get all lists and document libraries programmatically using CSOM in SharePoint Online
- To start seeing previews please log on by opening the document in SharePoint Online search result Web part
- The file filename is not checked out. You must first check out this document before making changes.
- Disable or Hide Sync Button at SharePoint Online Site and Document Libraries Level
- Content type Examples using CSOM in SharePoint Online/2013/2016
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.
I am Developer working on Microsoft Technologies for the past 6+years. I am very much passionate about programming and my core skills are SharePoint, ASP.NET & C#,Jquery,Javascript,REST. I am running this blog to share my experience & learning with the community I am an MCP, MCTS .NET & Sharepoint 2010, MCPD Sharepoint 2010, and MCSD HTML 5,Sharepoint 2013 Core
Solutions. I am currently working on Sharepoint 2010, MOSS 2007, Sharepoint 2013,Sharepoint 2013 App Dev, C#, ASP.NET, and SQL Server 2008.