如何使用 C# 读取 SharePoint Online 中单个文件的内容
How to read the contents of a single file in SharePoint Online using C#
我有以下 url,它在放入浏览器时显示文件的元数据,但我想要实际的文件内容。
https://mySite/_api/web/folders/getbyurl('Shared%20Documents')/folders/getbyurl('09.%20SharePoint%20Tutorials')/files/GetByUrl('SharePoint%20365%20Co-authoring%20excel%20files.docx')
我尝试执行以下操作,但在第 4 行执行查询时出现 400 错误:
ClientContext clientContext = new ClientContext("mySite");
File f = clientContext.Web.Folders.GetByUrl("Shared Documents").Folders.GetByUrl("09. SharePoint Tutorials").Files.GetByUrl("SharePoint 365 Co-authoring excel files.docx");
clientContext.Load(f);
clientContext.ExecuteQuery();
FileInformation fileInformation = File.OpenBinaryDirect(clientContext, (string)f.ServerRelativeUrl);
using (System.IO.StreamReader sr = new System.IO.StreamReader(fileInformation.Stream))
{
String line = sr.ReadToEnd();
Console.WriteLine(line);
}
以下代码供您参考:
string targetSiteURL = @"https://xx.sharepoint.com/sites/lz";
var login = "lz@xxx.onmicrosoft.com";
var password = "xxx";
var securePassword = new SecureString();
foreach (char c in password)
{
securePassword.AppendChar(c);
}
SharePointOnlineCredentials onlineCredentials = new SharePointOnlineCredentials(login, securePassword);
ClientContext ctx = new ClientContext(targetSiteURL);
ctx.Credentials = onlineCredentials;
var web = ctx.Web;
ctx.Load(web);
ctx.ExecuteQuery();
var filePath = web.ServerRelativeUrl + "/Shared%20Documents/09.%20SharePoint%20Tutorials/SharePoint%20365%20Co-authoring%20excel%20files.docx";
FileInformation fileInformation = Microsoft.SharePoint.Client.File.OpenBinaryDirect(ctx, filePath);
using (System.IO.StreamReader sr = new System.IO.StreamReader(fileInformation.Stream))
{
String line = sr.ReadToEnd();
Console.WriteLine(line);
}
Console.ReadKey();
我有以下 url,它在放入浏览器时显示文件的元数据,但我想要实际的文件内容。
https://mySite/_api/web/folders/getbyurl('Shared%20Documents')/folders/getbyurl('09.%20SharePoint%20Tutorials')/files/GetByUrl('SharePoint%20365%20Co-authoring%20excel%20files.docx')
我尝试执行以下操作,但在第 4 行执行查询时出现 400 错误:
ClientContext clientContext = new ClientContext("mySite");
File f = clientContext.Web.Folders.GetByUrl("Shared Documents").Folders.GetByUrl("09. SharePoint Tutorials").Files.GetByUrl("SharePoint 365 Co-authoring excel files.docx");
clientContext.Load(f);
clientContext.ExecuteQuery();
FileInformation fileInformation = File.OpenBinaryDirect(clientContext, (string)f.ServerRelativeUrl);
using (System.IO.StreamReader sr = new System.IO.StreamReader(fileInformation.Stream))
{
String line = sr.ReadToEnd();
Console.WriteLine(line);
}
以下代码供您参考:
string targetSiteURL = @"https://xx.sharepoint.com/sites/lz";
var login = "lz@xxx.onmicrosoft.com";
var password = "xxx";
var securePassword = new SecureString();
foreach (char c in password)
{
securePassword.AppendChar(c);
}
SharePointOnlineCredentials onlineCredentials = new SharePointOnlineCredentials(login, securePassword);
ClientContext ctx = new ClientContext(targetSiteURL);
ctx.Credentials = onlineCredentials;
var web = ctx.Web;
ctx.Load(web);
ctx.ExecuteQuery();
var filePath = web.ServerRelativeUrl + "/Shared%20Documents/09.%20SharePoint%20Tutorials/SharePoint%20365%20Co-authoring%20excel%20files.docx";
FileInformation fileInformation = Microsoft.SharePoint.Client.File.OpenBinaryDirect(ctx, filePath);
using (System.IO.StreamReader sr = new System.IO.StreamReader(fileInformation.Stream))
{
String line = sr.ReadToEnd();
Console.WriteLine(line);
}
Console.ReadKey();