Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用C在SharePoint Online中读取单个文件的内容#_C#_Sharepoint_Sharepoint Online_Csom - Fatal编程技术网

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

C# 如何使用C在SharePoint Online中读取单个文件的内容#,c#,sharepoint,sharepoint-online,csom,C#,Sharepoint,Sharepoint Online,Csom,我有下面的url,它在放入浏览器时显示文件的元数据,但我需要实际的文件内容 https://mySite/_api/web/folders/getbyurl('Shared%20Documents')/folders/getbyurl('09.%20SharePoint%20Tutorials')/files/GetByUrl('SharePoint%20365%20Co-authoring%20excel%20files.docx') 我已尝试执行以下操作,但在尝试执行第4行上的查询时,仅出

我有下面的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();

以下代码供您参考:

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

交换我得到的信息:System.Net.WebException:“远程服务器返回错误:(400)错误请求。”在ctx.ExecuteQuery()上;lineIt似乎我的问题是我制作了一个.net核心控制台程序,而不是.net framework控制台程序。谢谢你的帮助!交换我得到的信息:System.Net.WebException:“远程服务器返回错误:(400)错误请求。”在ctx.ExecuteQuery()上;lineIt似乎我的问题是我制作了一个.net核心控制台程序,而不是.net framework控制台程序。谢谢你的帮助!