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

后端 未结 8 2130
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-30 20:19

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?

相关标签:
8条回答
  • 2020-11-30 20:49

    This article describes various options for accessing SharePoint content. You have a choice between REST and CSOM. I'd try CSOM if possible. File upload / download specifically is nicely described in this article.

    Overall notes:

        //First construct client context, the object which will be responsible for
        //communication with SharePoint:
        var context = new ClientContext(@"http://site.absolute.url")
    
        //then get a hold of the list item you want to download, for example
        var list = context.Web.Lists.GetByTitle("Pipeline");
        var query = CamlQuery.CreateAllItemsQuery(10000);
        var result = list.GetItems(query);
    
        //note that data has not been loaded yet. In order to load the data
        //you need to tell SharePoint client what you want to download:
    
        context.Load(result, items=>items.Include(
            item => item["Title"],
            item => item["FileRef"]
        ));
    
        //now you get the data
        context.ExecuteQuery();
    
        //here you have list items, but not their content (files). To download file
        //you'll have to do something like this:
    
        var item = items.First();
    
        //get the URL of the file you want:
        var fileRef = item["FileRef"];
    
        //get the file contents:
        FileInformation fileInfo = File.OpenBinaryDirect(context, fileRef.ToString());
    
        using (var memory = new MemoryStream())
        {
              byte[] buffer = new byte[1024 * 64];
              int nread = 0;
              while ((nread = fileInfo.Stream.Read(buffer, 0, buffer.Length)) > 0)
              {
                  memory.Write(buffer, 0, nread);
              }
              memory.Seek(0, SeekOrigin.Begin);
              // ... here you have the contents of your file in memory, 
              // do whatever you want
        }
    

    Avoid working with the stream directly, read it into the memory first. Network-bound streams are not necessarily supporting stream operations, not to mention performance. So, if you are reading a pic from that stream or parsing a document, you may end up with some unexpected behavior.

    On a side note, I have a related question re: performance of this code above, as you are taking some penalty with every file request. See here. And yes, you need 4.5 full .NET profile for this.

    0 讨论(0)
  • 2020-11-30 20:49

    File.OpenBinaryDirect may cause exception when you are using Oauth accestoken Explained in This Article

    Code should be written as below to avoid exceptions

     Uri filename = new Uri(filepath);
            string server = filename.AbsoluteUri.Replace(filename.AbsolutePath, 
             "");
            string serverrelative = filename.AbsolutePath;
    
            Microsoft.SharePoint.Client.File file = 
            this.ClientContext.Web.GetFileByServerRelativeUrl(serverrelative);
            this.ClientContext.Load(file);
            ClientResult<Stream> streamResult = file.OpenBinaryStream();
            this.ClientContext.ExecuteQuery();
            return streamResult.Value;
    
    0 讨论(0)
  • 2020-11-30 20:53
    Private Sub DownloadFile(relativeUrl As String, destinationPath As String, name As String)
        Try
            destinationPath = Replace(destinationPath + "\" + name, "\\", "\")
            Dim fi As FileInformation = Microsoft.SharePoint.Client.File.OpenBinaryDirect(Me.context, relativeUrl)
            Dim down As Stream = System.IO.File.Create(destinationPath)
            Dim a As Integer = fi.Stream.ReadByte()
            While a <> -1
                down.WriteByte(CType(a, Byte))
                a = fi.Stream.ReadByte()
            End While
        Catch ex As Exception
            ToLog(Type.ERROR, ex.Message)
        End Try
    End Sub
    
    0 讨论(0)
  • 2020-11-30 20:53

    Just a suggestion SharePoint 2013 online & on-prem file encoding is UTF-8 BOM. Make sure your file is UTF-8 BOM, otherwise your uploaded html and scripts may not rendered correctly in browser.

    0 讨论(0)
  • 2020-11-30 20:58

    I would suggest reading some Microsoft documentation on what you can do with CSOM. This might be one example of what you are looking for, but there is a huge API documented in msdn.

    // Starting with ClientContext, the constructor requires a URL to the 
    // server running SharePoint. 
    ClientContext context = new ClientContext("http://SiteUrl"); 
    
    // Assume that the web has a list named "Announcements". 
    List announcementsList = context.Web.Lists.GetByTitle("Announcements"); 
    
    // Assume there is a list item with ID=1. 
    ListItem listItem = announcementsList.Items.GetById(1); 
    
    // Write a new value to the Body field of the Announcement item.
    listItem["Body"] = "This is my new value!!"; 
    listItem.Update(); 
    
    context.ExecuteQuery(); 
    

    From: http://msdn.microsoft.com/en-us/library/fp179912.aspx

    0 讨论(0)
  • 2020-11-30 21:02

    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);
         }
    }
    
    0 讨论(0)
提交回复
热议问题