如何解决如何使用 REST API 更新 OneDrive 项目的属性?
我正在尝试使用 SharePoint REST API 更新 OneDrive 项目的属性。
OneDrive 项目网址:
https://vx13-my.sharepoint.com/personal/alexw_vx13_onmicrosoft_com/Documents/SrcDir/File-To-Update.txt
主要希望更新以下字段:Author、Created_x0020_By、Editor、Modified_x0020_By、Modified、Created
我尝试使用图形 API:
Request:
Method: PATCH |
URL: https://graph.microsoft.com/v1.0/sites/vx13-my.sharepoint.com,eab581b1-a945-4d23-9c8e-ec67bb74a42d,32fa1468-54b6-40d6-abbd-f775c4c3932b/drives/b!sYG16kWpI02cjuxnu3SkLWgU-jK2VNZAq733dcTDkyvIAAMdpSy_Sryiw8ARQ8Gv/root:/SrcDir/File-To-Update.txt:/listItem/fields |
Header:
Content-Type: application/json |
Accept: application/json;odata=verbose |
Body:
{
"Created": "2019-02-01 10:25 AM","Modified": "2020-01-27 11:25 AM","Modified_x0020_By": "3","Created_x0020_By": "3","Author": "Alex Wilber","Editor": "Alex Wilber"
}
它给出的失败响应为:
{"code": "accessDenied","message": "Field 'Created' 是只读的"}
通过使用 ValidateUpdateListItem()
的 CSOM API 其工作并成功更新 OneDrive 项目的属性字段。
但是有没有办法通过 REST API 更新 OneDrive 项的属性字段?
解决方法
如果您参考 属性 部分下的文档 here,您会注意到某些字段是只读,您将无法使用图 API。
对于所有其他属性(例如名称),请按照此 documentation 进行更新。
话虽如此,您始终可以通过填写 user voice 来请求功能,以便它进入我们的待办事项。
如果有帮助,请点赞。谢谢!
,默认情况下,这些属性是只读的,不能编辑。
您需要将 ReadOnlyField 设置为 false 以更新这些字段。我曾经在这里回答过同样的问题:https://docs.microsoft.com/en-us/answers/questions/221106/rest-api-to-create-item-with-custom-created-by-and.html
对于创建者列:
Url: /_api/web/lists/getbytitle('Mylist')/fields/getbytitle('Created By')
Body:
{
"__metadata": { "type": "SP.FieldUser"},"ReadOnlyField": false
}
对于创建的列:
Url: /_api/web/lists/getbytitle('Mylist')/fields/getbytitle('Created')
Body:
{
"__metadata": { "type": "SP.FieldDateTime"},"ReadOnlyField": false
}
,
使用 SharePoint REST API, 使用方法 ValidateUpdateListItem()
更新 OneDrive 项的属性的解决方法获取文件的list-item-id
GET https://vx13-my.sharepoint.com/personal/alexw_vx13_onmicrosoft_com/_api/web/GetFileByServerRelativePath(decodedurl='/personal/alexw_vx13_onmicrosoft_com/Documents/SrcDir/File-To-Update.txt')/listitemallfields/fieldvaluesastext?$select=Id
使用 ValidateUpdateListItem 更新列表项的作者、编辑器、创建和修改字段
POST https://vx13-my.sharepoint.com/personal/alexw_vx13_onmicrosoft_com/_api/web/Lists/GetByTitle('Documents')/items(<list-item-id>)/ValidateUpdateListItem()
Content-Type:application/json
Accept:application/json;odata=verbose
Body:
{
"formValues":
[
{
"FieldName": "Created","FieldValue": "2019/02/01 10:25 AM"
},{
"FieldName": "Modified","FieldValue": "2020/01/27 11:25 AM"
},{
"FieldName": "Author","FieldValue": "[{'Key':'i:0#.f|membership|test_user_AK@vx13.onmicrosoft.com'}]"
},{
"FieldName": "Editor","FieldValue": "[{'Key':'i:0#.f|membership|test_user_JV@vx13.onmicrosoft.com'}]"
}
],"bNewDocumentUpdate": true
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。