gpt4 book ai didi

c# - 使用 C# 从 sharepoint 站点下载 excel 文件到本地系统

转载 作者:行者123 更新时间:2023-12-04 22:32:16 25 4
gpt4 key购买 nike

我需要将一个 Excel 文件从 sharepoint 下载到我的本地系统。
我写了下面的方法。

    internal static void DownloadFilesFromSharePoint(string siteUrl, string folderPath, string tempLocation)
{
ClientContext ctx = new ClientContext(siteUrl);
ctx.Credentials = new NetworkCredential("username", "password");

FileCollection files = ctx.Web.GetFolderByServerRelativeUrl(folderPath).Files;

ctx.Load(files);
ctx.ExecuteQuery();----FAILED HERE

foreach (File file in files)
{
FileInformation fileInfo = File.OpenBinaryDirect(ctx, file.ServerRelativeUrl);
ctx.ExecuteQuery();

var filePath = tempLocation + file.Name;
using (var fileStream = new System.IO.FileStream(filePath, System.IO.FileMode.Create))
{
fileInfo.Stream.CopyTo(fileStream);
}
}
}

要调用上述方法,我在 Main 方法中有以下内容:
    public static void Main()
{
Program.DownloadFilesFromSharePoint("http://sp.frk.com/teams/ResearchSystemsSupport/GT%20Resources/Forms/AllItems.aspx", "/TeamDocuments", @"c:\");

}

但是上面的代码不起作用。
我在调试时标记了代码给出异常的点(通过标记----FAILED HERE)。任何人都可以在这里指出问题。
任何人都可以帮我解决这个问题。如果可以的话,请给我一个详细的解释。

最佳答案

我终于弄清楚了粘贴在下面的代码。
正如 LZ_MSFT 在评论中指出的那样,我重新检查了我正在传递的共享点链接,他们错了,所以改变了它并且它起作用了。
同样在网络凭据中,添加了域。

    static void DownloadFilesFromSharePoint(string siteUrl, string siteFolderPath, string localTempLocation)
{
ClientContext ctx = new ClientContext(siteUrl);
ctx.Credentials = new NetworkCredential("username", "password", "Domain");

FileCollection files = ctx.Web.GetFolderByServerRelativeUrl(siteFolderPath).Files;

ctx.Load(files);
if (ctx.HasPendingRequest)
{
ctx.ExecuteQuery();
}

foreach (File file in files)
{
FileInformation fileInfo = File.OpenBinaryDirect(ctx, file.ServerRelativeUrl);
ctx.ExecuteQuery();

var filePath = localTempLocation + "\\" + file.Name;
System.IO.FileStream fileStream = new System.IO.FileStream(filePath, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.ReadWrite, System.IO.FileShare.ReadWrite);

fileInfo.Stream.CopyTo(fileStream);

}
}

调用函数:
    static void Main(string[] args)
{
Program.DownloadFilesFromSharePoint(@"http://sp.frk.com/teams/ResearchSystemsSupport", @"http://sp.frk.com/teams/ResearchSystemsSupport/Global%20Equity%20Group/Daily_Checks", @"C:\Temp");
}

关于c# - 使用 C# 从 sharepoint 站点下载 excel 文件到本地系统,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51845724/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com