无法将新文件 (<4MB) 上传到 SharePoint(ms graph api) [英] Unable to upload new file (&lt;4MB) to SharePoint (ms graph api)

查看:70
本文介绍了无法将新文件 (<4MB) 上传到 SharePoint(ms graph api)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将我从 Outlook 获取的附件上传到 SharePoint 文档库中的文件夹.我正在关注文档:https://docs.microsoft.com/en-us/graph/api/driveitem-put-content?view=graph-rest-1.0&tabs=http#http-request-to-upload-a-new-file

I'm trying to upload an attachment i get from Outlook to a folder inside a SharePoint document library. I am Following the docs: https://docs.microsoft.com/en-us/graph/api/driveitem-put-content?view=graph-rest-1.0&tabs=http#http-request-to-upload-a-new-file

fetch(`https://graph.microsoft.com/v1.0/sites/${siteId}/drive/items/${parentId}:/${attachment.name}:/content`, { 
      method: 'PUT', 
      mode: 'cors',
      headers: new Headers({
        'Authorization': `Bearer ${accesToken}`, 
        'Content-Type': 'text/plain'
      }),
      body: attachment.contentBytes
    })

我得到的只是代码错误:-1, Microsoft.SharePoint.Client.InvalidClientQueryException

All I get is an error with code: -1, Microsoft.SharePoint.Client.InvalidClientQueryException

出于测试目的,我尝试将 fetch 请求的正文设置为简单的字符串,例如hello world",但仍然出现相同的错误.

I've tried setting the body of the fetch request as a simple string such as "hello world" with testing purposes and still get the same error.

有什么想法吗?

提前谢谢

我怀疑我没有正确构建 URL.我还没有找到该参数的文档:

I suspect I'm not building the URL right. I haven't found the documentation for the parameter:

  • {item-id} 我假设这个 ID 是文件夹的 parentReference.siteId 属性.

是吗?

推荐答案

好的,在使用 Microsoft Graph Explorer 进行一些测试后,我发现将文件上传到位于文档库中的 SharePoint 文件夹的最简单方法(区别于根文档库)就是将其作为驱动器使用端点来处理:

Allright, so after some testing with the Microsoft Graph Explorer, I've found that the easiest way to upload a file to a SharePoint folder living inside a document library (distinct to the root document library) is to deal with it as a drive using the endpoint:

/drives/{drive-id}/items/{parent-id}:/{filename}:/content

(https://docs.microsoft.com/en-us/graph/api/driveitem-put-content?view=graph-rest-1.0&tabs=http#http-request-to-upload-a-new-file)

为了获取文档库的drive-id,您可以将odata参数$expand=drive添加到图形查询中,例如:

In order to get the document library's drive-id, you can append to the graph query the odata parameter $expand=drive like:

`https://graph.microsoft.com/v1.0/sites/${siteId}/lists?$expand=drive`

然后,在目标文档库的其他属性旁边,您将找到驱动器"对象,其中包含与您要将文件上传到的文档库相关联的 drive-id.因此,您可以像这样发出 PUT 请求:

Then, alongside other attributes of the targetted document library, you will find the "drive" object, which holds the drive-id associated to the document library you want to upload the file to. So, you would make the PUT request like:


fetch(`https://graph.microsoft.com/v1.0/drives/${libraryDriveId}/items/root:/${folderDisplayName}/${nameOfFile}:/content`, {
      method: 'PUT', 
      mode: 'cors',
      headers: new Headers({
        'Authorization': `Bearer ${accesToken}`, 
        'Content-Type': 'text/plain'
      }),
      body: BINARY_STREAM_OF_DATA
    }).then( (response) => {
      if (!response.ok) return response.json().then((json) => {throw json});
      return response.json();
    }).then( (json) => {
      //do whatever
    }).catch( (err) => {
      console.error(err);
    })

库驱动

  • libraryDriveId 来自 https://graph.microsoft.com/v1.0/sites/${siteId}/lists?$expand=drive 请求
  • /root:/${folderDisplayName} 表示您在文档库中定位的文件夹位于root:"(文档库的根目录)下,后跟 displayName 要将文件上传到的文件夹
  • nameOfFile 是你要上传的文件名
  • libraryDriveId comes from the https://graph.microsoft.com/v1.0/sites/${siteId}/lists?$expand=drive request
  • /root:/${folderDisplayName} means that the folder you are targetting inside the document library lives under "root:" (the root of the document library) followed by the displayName of the folder you want to upload the file to
  • nameOfFile is the name of the file you want to upload

这篇关于无法将新文件 (<4MB) 上传到 SharePoint(ms graph api)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆