简体   繁体   English

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

[英]How to read the contents of a single file in SharePoint Online using C#

I have the following url which displays the metadata of the file when put in the browser but I want the actual file content. 我有以下网址,该网址显示在浏览器中时文件的元数据,但我需要实际的文件内容。

https://mySite/_api/web/folders/getbyurl('Shared%20Documents')/folders/getbyurl('09.%20SharePoint%20Tutorials')/files/GetByUrl('SharePoint%20365%20Co-authoring%20excel%20files.docx')

I have tried doing the following but I just get a 400 error when trying to execute the query on line 4: 我尝试执行以下操作,但是在尝试在第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);
}

The code below for your reference: 以下代码供您参考:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 C# 在线将 email 发送给 SharePoint 中的多个用户 - How to send email to multiple user in SharePoint Online using C# 如何使用C#将文件上传到Sharepoint在线文档库 - How to upload files to sharepoint online document library using c# 如何获取 Sharepoint Online Migration API 日志(使用 c#) - How To Get Sharepoint Online Migration API Logs (using c#) 从 SharePoint 在线下载文件 C# - Download a File from SharePoint Online C# 访问 Sharepoint 上的数据/文件 c# 在线 - Access data/file on Sharepoint Online in c# 如何在 C# 中读取 SharePoint 在线文档中存在的 excel 文件,然后我必须加密一些列,最后将其写入 Snowflake 数据库? - How to read an excel file present in SharePoint online document in C#, then I have to encrypt some columns and finally write it to Snowflake database? c#-将在线文件的内容下载到字符串中 - c# - Download the contents of an online file to a string 使用 C# 为 Sharepoint 创建在线项目 - Creating a Project Online using C# for Sharepoint 如何使用 c# 以编程方式将文档从 Sharepoint 2010 迁移到 sharepoint online - How to migrate documents from Sharepoint 2010 to sharepoint online programatically using c# 使用FileStream读取文本文件的实际内容和这些选项c# - Read the actual contents of text file using FileStream and these options c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM