0
votes

I am trying to update the properties of a OneDrive item using SharePoint REST API.

OneDrive item web URL:

https://vx13-my.sharepoint.com/personal/alexw_vx13_onmicrosoft_com/Documents/SrcDir/File-To-Update.txt

Mainly looking to updating following fields: Author, Created_x0020_By, Editor, Modified_x0020_By, Modified, Created

I tried using Graph 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"
}

It is giving failure response as:

{"code": "accessDenied", "message": "Field 'Created' is read-only"}

Through CSOM API using ValidateUpdateListItem() its working & successfully updating the property fields of OneDrive items.

But is there any way to update property fields of OneDrive item through REST API?

3

3 Answers

0
votes

By default, these properties are Readonly by and cannot be edited.

You need to set the ReadOnlyField to false to update these fields. I used to answer the same question here: https://docs.microsoft.com/en-us/answers/questions/221106/rest-api-to-create-item-with-custom-created-by-and.html

For Created By column:

Url: /_api/web/lists/getbytitle('Mylist')/fields/getbytitle('Created By')

 Body:
     {
     "__metadata": { "type": "SP.FieldUser"},
     "ReadOnlyField": false
     } 

For Created column:

Url: /_api/web/lists/getbytitle('Mylist')/fields/getbytitle('Created')
 Body:
     {
     "__metadata": { "type": "SP.FieldDateTime"},
     "ReadOnlyField": false
     } 
0
votes

Using SharePoint REST API, a work around to update the Properties of OneDrive item using method ValidateUpdateListItem()

Get the list-item-id of the file

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

Update Author, Editor, Created & Modified fields of list item using 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 
}

0
votes

If you refer to the documentation here under the Properties section, you will notice certain fields are read only and you wont be able to update them using Graph API.

For all other properties(e.g. name), follow this documentation to update.

Being said that, you can always request for a feature by filling a User Voice so that it goes into our backlog.