I am developing a Win8 (WinRT, C#, XAML) client application (CSOM) that needs to download/upload files from/to SharePoint 2013.
How do I do the Download/Upload?
You can use the SharePoint client object model (CSOM) to retrieve, update, and manage data in SharePoint. SharePoint makes the CSOM available in several forms: . NET Framework redistributable assemblies.
NET client-side object model (CSOM), JavaScript object model (JSOM), and/or REST. This table lists the most frequently used core APIs, which are in most cases based on types from the . NET server implementation. In some cases, types are native to SharePoint client programming, and there is no equivalent .
Upload a file to a SharePoint site (including SharePoint Online) using File.SaveBinaryDirect Method:
using (var clientContext = new ClientContext(url)) { using (var fs = new FileStream(fileName, FileMode.Open)) { var fi = new FileInfo(fileName); var list = clientContext.Web.Lists.GetByTitle(listTitle); clientContext.Load(list.RootFolder); clientContext.ExecuteQuery(); var fileUrl = String.Format("{0}/{1}", list.RootFolder.ServerRelativeUrl, fi.Name); Microsoft.SharePoint.Client.File.SaveBinaryDirect(clientContext, fileUrl, fs, true); } }
Download file from a SharePoint site (including SharePoint Online) using File.OpenBinaryDirect Method:
using (var clientContext = new ClientContext(url)) { var list = clientContext.Web.Lists.GetByTitle(listTitle); var listItem = list.GetItemById(listItemId); clientContext.Load(list); clientContext.Load(listItem, i => i.File); clientContext.ExecuteQuery(); var fileRef = listItem.File.ServerRelativeUrl; var fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(clientContext, fileRef); var fileName = Path.Combine(filePath,(string)listItem.File.Name); using (var fileStream = System.IO.File.Create(fileName)) { fileInfo.Stream.CopyTo(fileStream); } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With