.Net C#脚本实现下载SharePoint文档中General文件夹中的的Excel文件到本地,文件名为SharepointTest.xlsx,文件连接https://sharewinfobi.sharepoint.com/:x:/s/AM1000/ETN_N3UL9thJgRHDPbun0UIBe-TqD_d-TF9kQft9aOKlog?e=b2OllY%E3%80%82
该回答引用于ChatGPT:
这里提供一个使用 Microsoft.SharePoint.Client 库实现从 SharePoint 中下载文件的 C# 代码示例:
using System;
using System.IO;
using Microsoft.SharePoint.Client;
class Program
{
static void Main(string[] args)
{
string siteUrl = "https://sharewinfobi.sharepoint.com/sites/AM1000";
string libraryName = "General";
string fileName = "SharepointTest.xlsx";
string localFilePath = @"C:\Downloads\" + fileName;
using (var clientContext = new ClientContext(siteUrl))
{
// Replace with the actual credentials of a user who has access to the SharePoint site
var credential = new SharePointOnlineCredentials("username@domain.com", GetSecureString("password"));
clientContext.Credentials = credential;
var web = clientContext.Web;
var list = web.Lists.GetByTitle(libraryName);
var query = CamlQuery.CreateAllItemsQuery();
query.ViewXml = "<View Scope='RecursiveAll'><Query><Where><Eq><FieldRef Name='FileLeafRef'/><Value Type='Text'>" + fileName + "</Value></Eq></Where></Query></View>";
var items = list.GetItems(query);
clientContext.Load(items, includes => includes.Include(item => item["FileLeafRef"],item => item.File));
clientContext.ExecuteQuery();
if (items.Count == 1)
{
var file = items[0].File;
clientContext.Load(file);
clientContext.ExecuteQuery();
var fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(clientContext, file.ServerRelativeUrl);
using (var fileStream = new FileStream(localFilePath, FileMode.Create))
{
fileInfo.Stream.CopyTo(fileStream);
}
Console.WriteLine("File downloaded successfully!");
}
else
{
Console.WriteLine("File not found or more than one file with the same name exists.");
}
}
}
public static System.Security.SecureString GetSecureString(string password)
{
var securePassword = new System.Security.SecureString();
foreach (char c in password)
{
securePassword.AppendChar(c);
}
return securePassword;
}
}
在上面的代码中,我们首先指定 SharePoint 网站的 URL、文档库的名称、要下载的文件名以及本地存储的路径。然后,我们使用 SharePointOnlineCredentials 类来设置用户凭据,这里需要用到 SharePoint 网站的用户名和密码。
接着,我们使用 ClientContext 对象来连接到 SharePoint 网站,并获取指定文档库中的所有项。这个查询使用了 CamlQuery,并被限制为只搜索指定的文件名。如果查询返回一个项目,我们可以打开二进制文件流并将其保存到本地文件中。
最后,我们在控制台输出成功或者失败的消息。
注意:在运行此代码之前,确保已经安装 Microsoft.SharePoint.Client 和 Microsoft.SharePoint.Client.Runtime 库。
该回答引用ChatGPTcsharp
string username = "yourUserName";
string password = "yourPassword";
SecureString securePassword = new SecureString();
foreach (char c in password)
{
securePassword.AppendChar(c);
}
SharePointOnlineCredentials credentials = new SharePointOnlineCredentials(username, securePassword);
csharp
string siteUrl = "https://sharewinfobi.sharepoint.com/sites/AM1000";
ClientContext context = new ClientContext(siteUrl);
context.Credentials = credentials;
csharp
string serverRelativeUrl = "/sites/AM1000/Shared Documents/General/SharepointTest.xlsx";
Microsoft.SharePoint.Client.File file = context.Web.GetFileByServerRelativeUrl(serverRelativeUrl);
context.Load(file);
context.ExecuteQuery();
string filePath = @"C:\temp\SharepointTest.xlsx";
using (var fileStream = System.IO.File.Create(filePath))
{
var fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(context, file.ServerRelativeUrl);
fileInfo.Stream.CopyTo(fileStream);
}
csharp
using Microsoft.SharePoint.Client;
using System;
using System.Security;
namespace SharePointFileDownloader
{
class Program
{
static void Main(string[] args)
{
string username = "yourUserName";
string password = "yourPassword";
SecureString securePassword = new SecureString();
foreach (char c in password)
{
securePassword.AppendChar(c);
}
SharePointOnlineCredentials credentials = new SharePointOnlineCredentials(username, securePassword);
string siteUrl = "https://sharewinfobi.sharepoint.com/sites/AM1000";
ClientContext context = new ClientContext(siteUrl);
context.Credentials = credentials;
string serverRelativeUrl = "/sites/AM1000/Shared Documents/General/SharepointTest.xlsx";
Microsoft.SharePoint.Client.File file = context.Web.GetFileByServerRelativeUrl(serverRelativeUrl);
context.Load(file);
context.ExecuteQuery();
string filePath = @"C:\temp\SharepointTest.xlsx";
using (var fileStream = System.IO.File.Create(filePath))
{
var fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(context, file.ServerRelativeUrl);
fileInfo.Stream.CopyTo(fileStream);
}
Console.WriteLine("File downloaded.");
Console.ReadLine();
}
}
}