如何使用office365 dll将文件上传到sharepoint

时间:2014-07-04 14:29:02

标签: c# sharepoint file-upload office365

我尝试使用以下dll将文件上传到OneDrive for Business / SharePoint:

  

Microsoft.Office.Oauth
  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);
    }
}

当我尝试运行此代码时出现此错误:

  

System.Exception:{“找不到实体”}   来源:Microsoft.Office365.SharePoint

我不明白的是我不需要SharePointClient(连接到sharepoint的客户端)对象来执行代码。
因为当我想获取所有文件的信息或者必须连接时,我使用这个对象。

_client = await EnsureClientCreated();
_client.Context.IgnoreMissingProperties = true;

那么如何将文件上传到OneDrive for Business / SharePoint?

2 个答案:

答案 0 :(得分:1)

我发现自己是一个解决方案,它比我想象的更简单:

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

答案 1 :(得分:0)