使用 C#.net 从 Sharepoint Online 下载文件?

Download file from Sharepoint Online using C#.net?

我想将 file\files 从 SharePoint 在线下载到我的本地,但我正在努力处理代码示例。我搜索了 google,但没有得到任何有价值的信息。

尝试下面的代码片段,使用 SharePoint Online CSOM 将文件从库下载到本地:

using Microsoft.SharePoint.Client;
using System.IO;
using System.Linq;
using System.Security;

namespace CSOM
{
    class Program
    {
        static void Main(string[] args)
        {

            using (ClientContext ctx = new ClientContext("https://tenantname.sharepoint.com/sites/sitename/"))
            {
                string password = "********";
                string account = "username@tenantname.onmicrosoft.com";
                var secret = new SecureString();
                foreach (char c in password)
                {
                    secret.AppendChar(c);
                }
                ctx.Credentials = new SharePointOnlineCredentials(account, secret);
                ctx.Load(ctx.Web);
                ctx.ExecuteQuery();

                List list = ctx.Web.Lists.GetByTitle("libraryTitle");

                FileCollection files = list.RootFolder.Folders.GetByUrl("/sites/sitename/shared documents/foldername").Files;

                ctx.Load(files);
                ctx.ExecuteQuery();

                foreach (Microsoft.SharePoint.Client.File file in files)
                {
                    FileInformation fileinfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(ctx, file.ServerRelativeUrl);

                    ctx.ExecuteQuery();

                    using (FileStream filestream = new FileStream("C:" + "\" + file.Name, FileMode.Create))
                    {
                        fileinfo.Stream.CopyTo(filestream);
                    }

                }
            };

        }


    }
}

以下 C# 程序适用于从 SharePoint 下载文件

使用 NuGet 包管理器,安装 Microsoft.SharePointOnline.CSOM

This package contains SharePoint and Client Object Model libraries

C#代码

using Microsoft.SharePoint.Client;
using System;
using System.IO;
using System.Linq;
using System.Security;

namespace SharePointFileDownlaload
{
    class Program
    {
        static void Main(string[] args)
        {
            string UserName = String.Empty;
            Console.Write("Type username and press enter: ");
            UserName = Console.ReadLine();

            string Pwd = String.Empty;
            Console.Write("Type password and press enter: ");
            Pwd = Console.ReadLine();

            string drive = String.Empty;
            Console.Write("Type the computer drive where you want to store the file and press enter: ");
            drive = Console.ReadLine();

            Console.WriteLine("Process started .... ");
           
            try
            {
                DownloadFilesFromSharePoint("MyFolder", UserName, Pwd, drive);
            }
            catch(Exception ex)
            {
                Console.WriteLine("ERROR: "+ex.Message);
            }

            Console.ReadLine();
        }

        private static void DownloadFilesFromSharePoint(string folderName, string UserName, string Pwd, string driveName)
        {
            //Load Libraries from SharePoint
            ClientContext ctxSite = GetSPOContext(UserName, Pwd);
            ctxSite.Load(ctxSite.Web.Lists);
            ctxSite.ExecuteQuery();

            Web web = ctxSite.Web;
            var docLibs = ctxSite.LoadQuery(web.Lists.Where(l => l.BaseTemplate == 101));  //DocumentLibrary only
            ctxSite.ExecuteQuery();

            foreach (var list in docLibs)
            {
                //Console.WriteLine(list.Title);
                ctxSite.Load(list.RootFolder.Folders);
                ctxSite.ExecuteQuery();

                string listTitle = list.Title;
                

                //Console.WriteLine("List Tile ------------------------------- " + listTitle);
                foreach (Folder folder in list.RootFolder.Folders)
                {
                    ctxSite.Load(folder.Files);
                    ctxSite.ExecuteQuery();

                    if (String.Equals(folder.Name, folderName, StringComparison.OrdinalIgnoreCase))
                    {
                        var folderDestination = driveName+@":\Test\SharePoint\" + listTitle + @"\" + folderName + @"\";
                        ctxSite.Load(folder.Files);
                        ctxSite.ExecuteQuery();

                        foreach (var file in folder.Files)
                        {
                            var fileName = Path.Combine(folderDestination, file.Name);
                            if (!System.IO.File.Exists(fileName))
                            {
                                Directory.CreateDirectory(folderDestination);
                                var fileRef = file.ServerRelativeUrl;
                                var fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(ctxSite, fileRef);
                                using (var fileStream = System.IO.File.Create(fileName))
                                {
                                    fileInfo.Stream.CopyTo(fileStream);
                                }
                            }
                        }
                        Console.WriteLine("Downloaded the file in " + folderDestination);
                    }

                }
                
            }
            
        }

        private static ClientContext GetSPOContext(string UserName, string Pwd)
        {
            
            string spsiteurl = "https://company.sharepoint.com/sites/YourSite/";  
            
            var secure = new SecureString();
            foreach (char c in Pwd)
            {
                secure.AppendChar(c);
            }
            ClientContext spoContext = new ClientContext(spsiteurl);
            spoContext.Credentials = new SharePointOnlineCredentials(UserName, secure);
            return spoContext;

        }

        private static void GetAllItemNamesInSP(string UserName, string Pwd)
        {
            //Load Libraries from SharePoint
            ClientContext ctxSite = GetSPOContext(UserName, Pwd);
            ctxSite.Load(ctxSite.Web.Lists);
            ctxSite.ExecuteQuery();

            foreach (List list in ctxSite.Web.Lists)
            {
                string nameTest = list.Title;
                string testVal = list.BaseType.ToString();
                Console.WriteLine(nameTest + " -------------- " + testVal);
                if (list.BaseType.ToString() == "DocumentLibrary")
                {
                }

            }
            Console.ReadLine();
        }

    }
}