无法将新文件 (<4MB) 上传到 SharePoint(毫秒图 api)
Unable to upload new file (<4MB) to 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
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
出于测试目的,我尝试将获取请求的主体设置为简单的字符串,例如 "hello world",但仍然出现相同的错误。
有什么想法吗?
提前致谢
[编辑]
我怀疑我没有正确构建 URL。
我还没有找到参数的文档:
- {item-id} 我假设这个 ID 是文件夹的
parentReference.siteId
属性。
对吗?
好吧,在使用 Microsoft Graph Explorer 进行一些测试后,我发现将文件上传到位于文档库(与根文档库不同)内的 SharePoint 文件夹的最简单方法是处理它作为使用端点的驱动器:
/drives/{drive-id}/items/{parent-id}:/{filename}:/content
为了获取文档库的 drive-id,您可以将 odata 参数 $expand=drive 添加到图形查询中,例如:
`https://graph.microsoft.com/v1.0/sites/${siteId}/lists?$expand=drive`
然后,除了目标文档库的其他属性外,您还会找到 "drive" 对象,其中包含与您想要的文档库关联的 drive-id将文件上传到。因此,您可以像这样发出 PUT 请求:
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
是你要上传的文件名
我正在尝试将从 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
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
出于测试目的,我尝试将获取请求的主体设置为简单的字符串,例如 "hello world",但仍然出现相同的错误。
有什么想法吗?
提前致谢
[编辑] 我怀疑我没有正确构建 URL。 我还没有找到参数的文档:
- {item-id} 我假设这个 ID 是文件夹的
parentReference.siteId
属性。
对吗?
好吧,在使用 Microsoft Graph Explorer 进行一些测试后,我发现将文件上传到位于文档库(与根文档库不同)内的 SharePoint 文件夹的最简单方法是处理它作为使用端点的驱动器:
/drives/{drive-id}/items/{parent-id}:/{filename}:/content
为了获取文档库的 drive-id,您可以将 odata 参数 $expand=drive 添加到图形查询中,例如:
`https://graph.microsoft.com/v1.0/sites/${siteId}/lists?$expand=drive`
然后,除了目标文档库的其他属性外,您还会找到 "drive" 对象,其中包含与您想要的文档库关联的 drive-id将文件上传到。因此,您可以像这样发出 PUT 请求:
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
是你要上传的文件名