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,

5 thoughts on “Two ways to Upload file from Client Object Model

  1. Hi Shakir Majeed,
    Your article helped me a lot . thank you sir. now I have a question.
    How can I set the metadatainformation such as “CreatedBy” and “ModifedBy” when I upload a new file using the ablove code?

    waiting for your reply,
    Thanks and Regards.

  2. Hi really this post help me a lot.But having some doubts on it. Can you please tell me what’s the libararyname, subfolderpath , and ileCreationInformation.Url means

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 )

Facebook photo

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

Connecting to %s