如何使用C#在SharePoint Online中读取单个文件的内容

时间:2018-05-14 20:21:57

标签: c# sharepoint sharepoint-online csom

我有以下网址,当放入浏览器时显示文件的元数据,但我想要实际的文件内容。

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);
}

1 个答案:

答案 0 :(得分:1)

以下代码供您参考:

wscript.shell