我需要一个API(C#)来将文件夹下载为SharePoint中的一个zip文件。选择一个或几个文件夹后,在SharePoint网站中启用此功能,但我找不到如何以编程方式执行此操作。
我没有在班级forEach
看到什么。也许以某种方式使用updateProperties
。找不到答案。我可以使用File.OpenBinaryDirect按文件执行此文件,但可能已存在就绪函数。感谢。
答案 0 :(得分:3)
示例代码供您参考。
static void Main(string[] args)
{
string _SiteUrl = "https://domain.sharepoint.com/sites/tst";
using (var clientContext = new ClientContext(_SiteUrl))
{
Console.ForegroundColor = ConsoleColor.Green;
string password = "pw";
SecureString sec_pass = new SecureString();
Array.ForEach(password.ToArray(), sec_pass.AppendChar);
sec_pass.MakeReadOnly();
clientContext.Credentials = new SharePointOnlineCredentials("lee@domain.onmicrosoft.com", sec_pass);
Web web = clientContext.Web;
Folder folder = web.GetFolderByServerRelativeUrl("/sites/TST/MyDoc4/Folder");
var files = folder.Files;
clientContext.Load(files);
clientContext.ExecuteQuery();
Console.WriteLine();
//Regex regex = new Regex(_SiteUrl, RegexOptions.IgnoreCase);
var list = web.Lists.GetByTitle("MyDoc4");
var libRootFolder = list.RootFolder;
var subFolder = libRootFolder.Folders.GetByUrl("Folder");
clientContext.Load(libRootFolder);
clientContext.Load(subFolder);
clientContext.ExecuteQuery();
Console.WriteLine(libRootFolder.ItemCount);
Console.WriteLine(subFolder.ItemCount);
using (System.IO.MemoryStream mStream = new System.IO.MemoryStream())
{
using (var archive = new ZipArchive(mStream, ZipArchiveMode.Create, true))
{
foreach (var file in files)
{
clientContext.Load(file);
Console.WriteLine(file.Name);
ClientResult<Stream> stream = file.OpenBinaryStream();
clientContext.ExecuteQuery();
var zipArchiveEntry = archive.CreateEntry(file.Name);
using (Stream zipEntryStream = zipArchiveEntry.Open())
{
if (stream != null)
{
stream.Value.CopyTo(zipEntryStream);
}
}
}
}
using (var fileStream = new FileStream(@"C:\Lee\FileDownload\test.zip", FileMode.Create))
{
mStream.Seek(0, SeekOrigin.Begin);
mStream.CopyTo(fileStream);
}
}
Console.WriteLine("done");
Console.ReadKey();
}
}