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( subfolderPath)) {uploadLocation = string.Format("/{0}/{1}/", libraryName, subfolderPath);} else { 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; list.RootFolder.Files.Add(fileCreationInformation); clientContext.ExecuteQuery(); #endregion #region Uploading directly using (FileStream fs = new FileStream(fileName, FileMode.Open)) { Microsoft.SharePoint.Client.File.SaveBinaryDirect(clientContext, uploadLocation, fs, true); } #endregion } }
Please don’t forget to cast your opinion below about this blog
Cheers,
Happy SharePointing,
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.
Thanks mate,
Please follow this article,
https://junooni.wordpress.com/2011/10/04/how-to-find-the-listitem-metadata-info-client-object-model/
it will be set automatically.
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
There are loads of examples available 🙂