In this article we explored how to upload a document from physical location to SharePoint Document library using client side object model. This can be achieved using server side or SharePoint Web Services. Many times there is a requirement to upload a document from a physical path to document library. We will explore it using Client Side Object model (csom) in SharePoint 2013.
New to Office 365 SharePoint Online? Get Office 365 Enterprise E3 Subscription & Try out all the features
Read some SharePoint 2013 tutorials below:
- CRUD operations in SharePoint 2013 list item using rest api
- CRUD operations in SharePoint 2013 list using Web Services and jQuery
- CRUD operations in SharePoint 2013 list using jsom
Before:

Solution:
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:

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