使用C#从Sharepoint下载多个文件

时间:2018-04-01 16:04:53

标签: c# api sharepoint download

我必须检查包含许多文件的SharePoint网站,并将SharePoint网站中存在的所有Excel文件下载到本地计算机中的特定文件夹。我正在使用Microsoft.SharePoint.Client DLL。关于如何做到这一点的任何代码片段都会有用吗?

1 个答案:

答案 0 :(得分:0)

您的示例代码。

using (var context = new ClientContext("https://domain.sharepoint.com/sites/TST"))
            {                 
                Console.ForegroundColor = ConsoleColor.Green;
                string password = "pw";
                SecureString sec_pass = new SecureString();
                Array.ForEach(password.ToArray(), sec_pass.AppendChar);
                sec_pass.MakeReadOnly();
                context.Credentials = new SharePointOnlineCredentials("lee@domain.onmicrosoft.com", sec_pass);

                CamlQuery myquery = new CamlQuery();
                myquery.ViewXml = "<View Scope=\"RecursiveAll\">" +
                "<Query>"+
                "<Where>" +
                   "<Eq>" +
                        "<FieldRef Name='DocIcon' />" +
                         "<Value Type='Computed'>xls</Value>" +
                   "</Eq>" +
                "</Where>"+
                "</Query>"+
                 "</View>";
                var docLibs = context.LoadQuery(context.Web.Lists.Where(l => l.BaseTemplate == 101));
                context.ExecuteQuery();
                foreach (var list in docLibs)
                {
                    var result = list.GetItems(myquery);
                    context.Load(result, items => items.Include(
                        item => item["Title"],
                        item => item["FileRef"]
                    ));
                    //context.Load(result);
                    context.ExecuteQuery();
                    foreach(var file in result){                        
                        var fileRef = file["FileRef"];                        
                        FileInformation fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(context, fileRef.ToString());
                        context.ExecuteQuery();

                        var filePath = @"C:\Lee\FileDownload\" + fileRef.ToString().Split('/').Last();
                        using (var fileStream = new System.IO.FileStream(filePath, System.IO.FileMode.Create))
                        {
                            fileInfo.Stream.CopyTo(fileStream);
                        }
                    }                    
                }

                Console.WriteLine("done");
                Console.ReadKey();
            }