Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to download/upload files from/to SharePoint 2013 using CSOM?

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?

like image 869
eitan barazani Avatar asked Jun 12 '13 03:06

eitan barazani


People also ask

What is Csom in SharePoint 2013?

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.

What is Csom and JSOM in SharePoint?

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 .


1 Answers

Upload a file

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

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);      } } 
like image 168
Vadim Gremyachev Avatar answered Sep 23 '22 06:09

Vadim Gremyachev