Alex Lazo
Alex Lazo

Reputation: 126

How to create a file in a team drive using Google Drive API

I would like to figure how to create a file within a Team drive using Google's Drive API.

Here is a reference to for files and teamdrives in google drive api's documentation.

https://developers.google.com/drive/api/v3/reference/files

https://developers.google.com/drive/api/v3/reference/files/create

https://developers.google.com/drive/api/v3/enable-shareddrives

const resource = {
    name: fileName,
    supportsAllDrives: true,
    driveId: TEAMDRIVE_ID,
    mimeType: 'application/vnd.google-apps.spreadsheet'
};

drive.files.create(
   {
       resource,
       fields: 'id, name, webViewLink'
    },
    (err, file) => {
        if (err) {
            return({ msg: 'Failed Creating the file', err });
        } else {

            return file.data;
        }
     }
);

The code is able to create the file, but instead of it appearing in the team drive. It appears inside my personal drive. I am able to read files within my team drive. Creating files has been an issue for me though...

Upvotes: 4

Views: 2264

Answers (1)

Alex Lazo
Alex Lazo

Reputation: 126

I ended up finding an answer to my question, here is what worked for me.

const resource = {
    name: fileName,
    driveId: TEAMDRIVE_ID,
    mimeType: 'application/vnd.google-apps.spreadsheet',
    parents: [parent_id]
};

drive.files.create(
   {
       resource,
       fields: 'id, name, webViewLink'
       supportsAllDrives: true,
    },
    (err, file) => {
        if (err) {
            return({ msg: 'Failed Creating the file', err });
        } else {

            return file.data;
        }
     }
);

I had to move the supportsAllDrives: true out of the resource object and move it as an option in the drive.files.create paramter.

Also needed to add parents to the resource object. The parent_id can be a Team Drive Id or a folder Id within the Team Drive.

Upvotes: 7

Related Questions