如何使用 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 
}

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...