如何使用office365 dll上传文件到sharepoint ?
本文关键字:文件 sharepoint 何使用 office365 dll | 更新日期: 2023-09-27 18:11:32
我尝试使用以下dll将文件上传到OneDrive for Business/SharePoint:
Microsoft.Office。
Microsoft.Office365.SharePoint;
Microsoft.Office365.SharePoint.Extensions;
,代码如下:
public async void UploadDataAsync(byte[] bytes, string fileName)
{
// NAME: TestFolder, ID: TestFolder, URL: /personal/[company]_onmicrosoft_com/Documents/TestFolder, SIZE: 0
UserInformation userInformation = new UserInformation();
userInformation.Id = "Castrovalva";
userInformation.Name = "Castrovalva";
Microsoft.Office365.SharePoint.File file = new Microsoft.Office365.SharePoint.File();
file.CreatedBy = userInformation;
file.LastModifiedBy = userInformation;
file.Id = fileName;
file.Name = fileName;
file.Url = "/personal/[company]_onmicrosoft_com/Documents/TestFolder";
file.TimeCreated = DateTime.Now;
file.TimeLastModified = DateTime.Now;
file.ETag = "ETAG";
using (MemoryStream memory = new MemoryStream(bytes))
{
await file.UploadAsync(memory);
}
}
当我尝试运行这段代码时,我得到了这个错误:
系统。异常:{"cannot find entity"}
来源:Microsoft.Office365。SharePoint
我不明白的是,我不需要SharePointClient(客户端连接到sharepoint)对象来执行代码。
因为当我想要获取所有文件的信息或者需要连接时,我会使用这个对象。
_client = await EnsureClientCreated();
_client.Context.IgnoreMissingProperties = true;
那么我如何上传文件到OneDrive for Business/SharePoint?
我找到了一个解决方案,它比我想象的要简单得多:
using (Stream stream = new MemoryStream(bytes))
{
try
{
//await _client.Context.ExecuteAsync(uri, "POST", stream);
await _client.Files.AddAsync(fileName, true, stream);
}
catch (InvalidOperationException exception)
{ MessageBox.Show(exception.Message); }
}