简体   繁体   English

MSGraph/GraphServiceClient OneDrive 使用 C# 将文件上传到特定站点和/或文件夹

[英]MSGraph/GraphServiceClient OneDrive uploading file to specific Site and/or Folder with C#

Blockquote块引用

I have the following code which works fine and uploads the file to the Root Shared Directory.我有以下代码可以正常工作并将文件上传到根共享目录。

    var uploadedFile = await graphClient.Drive.Root
                                  .ItemWithPath(fileName)
                                  .Content
                                  .Request()
                                  .PutAsync<DriveItem>(fileStream);

However, I haven't been able to work out, how to upload a file to another Site or Folder created in SharePoint/OneDrive.但是,我一直无法弄清楚如何将文件上传到在 SharePoint/OneDrive 中创建的另一个站点或文件夹。

Here is the code I am trying to use where I have created a new Communication Site in Sharepoint named "DocumentUpload" but it errors.这是我尝试使用的代码,我在 Sharepoint 中创建了一个名为“DocumentUpload”的新通信站点,但它出错了。

    var uploadedFile = await graphClient.Sites["DocumentUpload"].Drive.Root
                                  .ItemWithPath(fileName)
                                  .Content
                                  .Request()
                                  .PutAsync<DriveItem>(fileStream);

When I use the above code, I get the following error:当我使用上面的代码时,出现以下错误:

Microsoft.Graph.ServiceException: 'Code: invalidRequest Message: Invalid hostname for this tenancy Inner error: AdditionalData: date: 2022-02-28T13:17:25 request-id: {Guid} client-request-id: {Guid} ClientRequestId: {Guid} ' Microsoft.Graph.ServiceException:'代码:无效请求消息:此租赁的主机名无效内部错误:附加数据:日期:2022-02-28T13:17:25请求ID:{Guid}客户端请求ID:{Guid} ClientRequestId : {指南} '

Any help would be greatly appreciated.任何帮助将不胜感激。

In order to get the Site Id, I had to do a GET request where I pass in the hostname eg xxxx.sharepoint.com and the relative-path eg sites/DocumentUpload as shown below.为了获得站点 ID,我必须执行 GET 请求,其中我传入主机名(例如 xxxx.sharepoint.com)和相对路径(例如 sites/DocumentUpload),如下所示。

Then I get a response back which I can use in the graphClient.Sites call I was confused about where I was passing in "DocumentUpload" instead of the Site Id which ends up looking like this "xxxx.sharepoint.com,{Guid},{Guid}"然后我得到一个回复,我可以在 graphClient.Sites 调用中使用我对我在“DocumentUpload”而不是站点 ID 中传递的位置感到困惑,它最终看起来像这样“xxxx.sharepoint.com,{Guid},{Guid }”

    using (var request = new HttpRequestMessage(new HttpMethod("GET"), $"https://graph.microsoft.com/v1.0/sites/xxxx.sharepoint.com:/sites/DocumentUpload"))
    {
        request.Headers.TryAddWithoutValidation("Authorization", $"Bearer {_authResult.AccessToken}");
        var response = await httpClient.SendAsync(request);
        var siteDetails = JsonConvert.DeserializeObject<SiteDetails>(response.Content.ReadAsStringAsync().Result);

        ...

        if (fileInfo.Length < LARGE_FILE_SIZE)
        {
            // upload the file to OneDrive
            var uploadedFile = await graphClient.Sites[siteDetails.id].Drive.Root
                                          .ItemWithPath(fileName)
                                          .Content
                                          .Request()
                                          .PutAsync<DriveItem>(fileStream);
        }
    }
...
    class SiteDetails
    {
        public string id { get; set; }
        public string displayName { get; set; }
        public string name { get; set; }
        public DateTime createdDateTime { get; set; }
        public DateTime lastModifiedDateTime { get; set; }
        public string webUrl { get; set; }
    }

After making this change, the Document uploads fine to that path.进行此更改后,文档可以正常上传到该路径。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM