SharePoint Online CSOM 的备选 Save/OpenBinaryDirect 方法
Alternative Save/OpenBinaryDirect methods for CSOM for SharePoint Online
根据 MS https://docs.microsoft.com/en-us/sharepoint/dev/sp-add-ins/using-csom-for-dotnet-standard 的文档,Save/OpenBinaryDirect 方法不适用于 .NET 核心应用程序,他们建议使用常规文件 API,那么 read/write 文件存储在 SharePoint online 中的替代方法是什么?什么是常规文件 API?有人做过吗?任何例子 code/documentation?
在 .NET Core CSOM 中下载文件:
using (var authenticationManager = new AuthenticationManager())
using (var context = authenticationManager.GetContext(site, user, password))
{
context.Load(context.Web, p => p.Title);
context.ExecuteQuery();
Microsoft.SharePoint.Client.File file = context.Web.GetFileByUrl("https://tenant.sharepoint.com/sites/michael/Shared%20Documents/aa.txt");
context.Load(file);
context.ExecuteQuery();
string filepath = @"C:\temp\" + file.Name;
Microsoft.SharePoint.Client.ClientResult<Stream> mstream = file.OpenBinaryStream();
context.ExecuteQuery();
using (var fileStream = new System.IO.FileStream(filepath, System.IO.FileMode.Create))
{
mstream.Value.CopyTo(fileStream);
}
using (System.IO.StreamReader sr = new System.IO.StreamReader(mstream.Value))
{
String line = sr.ReadToEnd();
Console.WriteLine(line);
}
}
在 .NET Core CSOM 中上传文件:
string filepath = @"C:\temp\aa.txt";
FileCreationInformation newfile = new FileCreationInformation();
newfile.Url = System.IO.Path.GetFileName(filepath);
newfile.Content= System.IO.File.ReadAllBytes(filepath);
List library = context.Web.Lists.GetByTitle("Documents");
Microsoft.SharePoint.Client.File uploadFile = library.RootFolder.Files.Add(newfile);
context.Load(uploadFile);
context.ExecuteQuery();
根据 MS https://docs.microsoft.com/en-us/sharepoint/dev/sp-add-ins/using-csom-for-dotnet-standard 的文档,Save/OpenBinaryDirect 方法不适用于 .NET 核心应用程序,他们建议使用常规文件 API,那么 read/write 文件存储在 SharePoint online 中的替代方法是什么?什么是常规文件 API?有人做过吗?任何例子 code/documentation?
在 .NET Core CSOM 中下载文件:
using (var authenticationManager = new AuthenticationManager())
using (var context = authenticationManager.GetContext(site, user, password))
{
context.Load(context.Web, p => p.Title);
context.ExecuteQuery();
Microsoft.SharePoint.Client.File file = context.Web.GetFileByUrl("https://tenant.sharepoint.com/sites/michael/Shared%20Documents/aa.txt");
context.Load(file);
context.ExecuteQuery();
string filepath = @"C:\temp\" + file.Name;
Microsoft.SharePoint.Client.ClientResult<Stream> mstream = file.OpenBinaryStream();
context.ExecuteQuery();
using (var fileStream = new System.IO.FileStream(filepath, System.IO.FileMode.Create))
{
mstream.Value.CopyTo(fileStream);
}
using (System.IO.StreamReader sr = new System.IO.StreamReader(mstream.Value))
{
String line = sr.ReadToEnd();
Console.WriteLine(line);
}
}
在 .NET Core CSOM 中上传文件:
string filepath = @"C:\temp\aa.txt";
FileCreationInformation newfile = new FileCreationInformation();
newfile.Url = System.IO.Path.GetFileName(filepath);
newfile.Content= System.IO.File.ReadAllBytes(filepath);
List library = context.Web.Lists.GetByTitle("Documents");
Microsoft.SharePoint.Client.File uploadFile = library.RootFolder.Files.Add(newfile);
context.Load(uploadFile);
context.ExecuteQuery();