Search code examples
azuremicrosoft-graph-apioffice365onedrive

Graph API: Unable to programmatically upload OneDrive item with specific properties (failing for createdBy and lastModifiedDataTime)


I am uploading an item to OneDrive using Graph API. I am also setting the properties of the item after it has been uploaded successfully. I am able to set "lastModifiedDateTime" but am not able to set "createdBy" and "createdDateTime".

"createdBy" is always set to the Azure AD application I have created for OAuth and in OneDrive UI it always shows "modified By" "SharePoint App".

And the "createdDataTime" is always current time (time of upload). Is there any way I can set these properties correctly?

The json I am using to patch the item properties:

{"createdDateTime":"2020-12-28T12:25:39Z",
  "lastModifiedDateTime":"2020-12-28T12:25:39Z",
  "createdBy":
  {
    "user":{
     "email":"AlexW@vx2.onmicrosoft.com"}
  },
  "lastModifiedBy":{
  "user":{
      "email":"AlexW@vx2.onmicrosoft.com"}
   },
   "fileSystemInfo":{
   "lastModifiedDateTime":"2020-12-28T12:25:39Z",
   "createdDateTime":"2020-12-28T12:25:39Z"},
   "file":{"mimeType":"image/jpeg"}
  }

Please find the properties (queries from graph explorer) after the upload and above patch request:

        {
          "createdDateTime": "2020-12-28T12:28:09Z",
          "lastModifiedDateTime": "2020-12-28T12:25:39Z",
          "createdBy": 
           {
              "application": {
              "displayName": "ConsoleApp"}
           },
           "fileSystemInfo": {
              "createdDateTime": "2020-12-28T12:28:09Z",
              "lastModifiedDateTime": "2020-12-28T12:25:39Z"
           },
           "file": {
              "mimeType": "image/jpeg",
              "hashes": {
                 "quickXorHash": "4EQEGnBnLd04VXEmYqGHHIeZ2po="
            }
           }
          }

As you can see user name has been replaced by the Azure AD app name and created by time is the time the upload was done and not the time specified in the patch request.

Please let me know if anyone has any idea about this.


Solution

  • If you refer the below article : https://learn.microsoft.com/en-us/graph/api/resources/driveitem?view=graph-rest-1.0 under the Properties section.

    enter image description here

    These are read-only fields meaning you will not be able to manually configure the values for the same.

    WorkAround :

    Having said that this cannot be achieved through Graph API however, you can make use of the Sharepoint API to update the same.

    ValidateUpdateListItem()

    For modifiying the created by , last modified by and last modified the sample body would be of below :

    {   formValues": [
    
                {
    
                   "FieldName": "Editor",
                   "FieldValue": "[{'Key':'i:0#.w|AlexW@vx2.onmicrosoft.com'}]"
    
                },
    
             {  
    
                   "FieldName": "Author",
                   "FieldValue": "[{'Key':'i:0#.w|AlexW@vx2.onmicrosoft.com'}]"
    
                },
                {
    
                   "FieldName": "Created",
                   "FieldValue": "02/18/2020 11:25 PM"
    
                }
    ],
      "bNewDocumentUpdate": true 
    }
     
    

    Request URL :

    https://SPOURL/_api/web/Lists/GetbyTitle('Library Name')/items(1)/ValidateUpdateListItem"