Two ways to Upload file from Client Object Model

Hi Guys,

Just  a quick example, Take a scenario that you are creating an Application (in our case WinForm Application). and you want to upload a document to document library from that application, there are two ways to do, One is to upload from filecreation and the other one is to directly upload with file.savebinary method.

public void uploadfile(string siteurl,string fileName,string libraryName, string subfolderPath)

using (ClientContext clientContext = new ClientContext(siteUrl))

string uploadLocation = string.Empty;
if (!string.IsNullOrEmpty(
{uploadLocation = string.Format("/{0}/{1}/", libraryName, subfolderPath);}
uploadLocation = string.Format("/{0}/", libraryName);

#region Upload through filecreation
var fileCreationInformation = new FileCreationInformation();

byte[] bytefile = System.IO.File.ReadAllBytes(fileName);
fileCreationInformation.Content = bytefile;
fileCreationInformation.Overwrite = true;
uploadLocation = uploadLocation + Path.GetFileName(fileName);
fileCreationInformation.Url = uploadLocation;


#region Uploading directly
using (FileStream fs = new FileStream(fileName, FileMode.Open))

Microsoft.SharePoint.Client.File.SaveBinaryDirect(clientContext, uploadLocation, fs, true);






Please don’t forget to cast your opinion below about this blog
Happy SharePointing,

About Shakir Majeed

I am SharePoint certified Specialist, SharePoint Trainer, Techie Speaker. I am in-list of Speakers on SharePoint from Microsoft Innovation Center Karachi Pakistan. I am User Group Leader of SharePoint Techies User Group(STUG), View all posts by Shakir Majeed

2 responses to “Two ways to Upload file from Client Object Model

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: