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

时间:2019-09-20 19:52:20

标签: c# c#-4.0 sharepoint sharepoint-online

我想从sharepoint在线将文件\文件下载到本地,而我在代码示例中苦苦挣扎。我在Google上搜索,但没有得到任何有价值的信息。请问您能为此建议任何代码段吗?感谢您的提前帮助。

2 个答案:

答案 0 :(得分:0)

尝试下面的代码段,使用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);
                    }

                }
            };

        }


    }
}

答案 1 :(得分:0)

以下 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();
        }

    }
}