SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need to upload a document to a SharePoint list or folder using the Client Side Object Model from .NET (C#). What is the best way to do this? The requirements are as follows:

  • Set metadata values
  • No limitation on file size
  • Must work with libraries that exceed the List View Threshold
share|improve this question
           SPFile file = oListItem.File;

           StreamReader fsReader = new StreamReader(file.OpenBinaryStream());
           byte[] contents = null;
           using (Stream fStream = fsReader.BaseStream)
               contents = new byte[fStream.Length];
               fStream.Read(contents, 0, (int)fStream.Length);

           ClientContext clientContext = new ClientContext("SITE URL");
           Web _Site = clientContext.Web;
           List _List = _Site.Lists.GetByTitle("Shared Documents");

           FileCreationInformation newFile = new FileCreationInformation();
           newFile.Content = contents;  //bytes here
           newFile.Url = _FileName;
           newFile.Overwrite = true;

           Microsoft.SharePoint.Client.File _UploadingFile =                


           CamlQuery _CamlQuery = new CamlQuery();
           _CamlQuery.ViewXml = "<View><Query><Where><Contains><FieldRef Name='FileLeafRef'/><Value Type='Text'>" + _Name + "</Value>" +
           ListItemCollection _ListItemCollection = _List.GetItems(_CamlQuery);

           int _count = _ListItemCollection.Count;

           ListItem _ListItem = _ListItemCollection[0];
           _ListItem["ColName1"] = ColValue1;
           _ListItem["ColName2"] = ColValue2;


share|improve this answer
ClientContext ctx = new ClientContext("share point url");//httP:// library
FileCreationInformation fci = new FileCreationInformation();
                    fci.Content = System.IO.File.ReadAllBytes("Your File Physical path");//D:\ filename
                    fci.Url = "SP-UploadTest";
                    fci.Overwrite = true;
                    Microsoft.SharePoint.Client.File fileToUpload = folder.Files.Add(fci);
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.