Ben Teater
Ben Teater

Reputation: 13

Drive API Update File in Shared Drive

I'm trying to use the Google Drive API to update a file in a shared drive using Javascript. This code worked fine in testing in my personal drive, but upon moving it to a team drive I'm getting errors. I found I needed to add SupportsAllDrives to get it to recognize the file, but now it returns the error "Execution failed: Object does not allow properties to be added or changed" when trying to update. Happy to post more code but I feel like I'm missing something in the drive.files.update section to allow it to update a shared file.

Again, this works on a personal drive so I'm confident the rest of the code is correct, I just can't find how else to modify it to allow it to work with a shared drive.

Thanks in advance for the help, I can't find much documentation on this.

      // Updates file metadata and/or content with the Drive API
         Drive.Files.update({
         title: file.getName(), mimeType: file.getMimeType(), driveId: RESULTS_FOLDER_ID, parents: RESULTS_FOLDER_ID
      }, fileId=file.getId(), body=fileBlob.copyBlob(), file.SupportsAllDrives=true);
    }

Upvotes: 1

Views: 1260

Answers (1)

carlesgg97
carlesgg97

Reputation: 4430

Assuming that you are using Google Apps Script's Advanced Drive Service which you seem to be using, your request should look like the following:

Drive.Files.update({
    title: file.getName(),
    mimeType: file.getMimeType(),
    driveId: RESULTS_FOLDER_ID,
    parents: RESULTS_FOLDER_ID
  },
  file.getId(),
  fileBlob.copyBlob(),
  {supportsAllDrives:true}
);

Note that the definition of update is (as seen on the IDE's autocomplete):

update(File resource, String fileId, Blob mediaData, Object optionalArgs) : File

In your case you seem to be using Python-style named arguments, however in Javascript there is no such thing. Also, the 4th argument must be an object.

Upvotes: 2

Related Questions