this is my first time submitting a question, so please help me if I could ask this better.
I want to:
- create a folder
- in a google team drive
- nested in an existing Team Drive folder “path”
- from Google Apps Script, which can be in a Script file or attached to a Google Sheet
I believe these resources tell me how to create the Team Drive folder:
- https://developers.google.com/drive/v3/reference/files/create
- https://developers.google.com/drive/v3/web/simple-upload
- https://developers.google.com/drive/v3/web/folder
Plan A: Create a folder using GAS. After some research, it’s my understanding that plain GAS cannot create a folder in Team Drive yet.
[18-03-15 14:55:23:674 EDT] Execution failed: Cannot use this operation on a Team Drive item. (line 139, file "utils") [1.618 seconds total runtime]
//The folder with the current name doesn't exist - create it in the parent folder
var newFolder = DriveApp.createFolder(nextFolderName);
currFolder.addFolder(newFolder);
If I’m wrong and there is a simple way to make the addFolder() code work with Team Drive, then that would be a win.
Update 1: You can create a folder in Team Drive with plain GAS. It is folder moving that is restricted. This works:
//The folder with the current name doesn't exist - create it in the parent folder
var newFolder = currFolder.createFolder(nextFolderName);
So Plan B: Now, I’m trying to create the folder using Google Drive REST API v3. However, I’m only getting an Untitled file in my personal drive when I run my code, even though TEAM_ROOT_PATH_FOLDER_ID is in a Team Drive. I think my problem is getting the right information to UrlFetchApp.fetch. It’s like the request body isn’t being read.
References:
- Google Apps Script: UrlFetchApp Post File
- https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app
Here’s my code for creating the folder using Google Apps Script and the Drive REST API:
var TEAM_ROOT_PATH_FOLDER_ID = "diPcXqabvgpX4tcZJZ-ms2Wg0bXf1Fem"; //TODO fill in based on parent folder
function test() {
createTeamDriveFolder(TEAM_ROOT_PATH_FOLDER_ID);
}
function createTeamDriveFolder(parentFolderId) {
//BUILD THE URL
var baseUrl = "https://www.googleapis.com/upload/drive/v3/files/";
var urlParams = { //TODO correct for this function (after you figure this out)
supportTeamDrives: true
//,alt: "json"
,uploadType: "media"
};
//CODE CREDIT: https://ctrlq.org/code/20514-list-team-drives-google-drive-apps-script
var queryString = Object.keys(urlParams).map(function(p) {
return [encodeURIComponent(p), encodeURIComponent(urlParams[p])].join("=");
}).join("&");
var apiUrlStr = baseUrl + "?" + queryString;
//BUILD THE REQUEST - HEADERS AND BODY
var token = ScriptApp.getOAuthToken();
var requestBody = {
mimeType: "application/vnd.google-apps.folder"
,name: "TeamDriveTestFolder"
,"parents": [parentFolderId]
};
var requestBodyStr = JSON.stringify(requestBody);
Logger.log("requestBody: " + requestBodyStr);
var requestParams = {
method: "POST"
,contentType: "application/json"
,contentLength: requestBodyStr.length
,headers: {"Authorization": "Bearer " + token}
,payload: requestBodyStr
};
var response = UrlFetchApp.fetch(apiUrlStr, requestParams);
Logger.log(response.getResponseCode());
Logger.log(response.getContentText());
}
However, I can get a folder to appear in my personal drive (still wrong) when I run the REST call through the API test:
supportsTeamDrives > true
WORKS (TeamDriveTestFolder folder created in personal drive root):
{
"mimeType": "application/vnd.google-apps.folder",
"name": "TeamDriveTestFolder"
}
FAILS (404):
{
"mimeType": "application/vnd.google-apps.folder",
"name": "TeamDriveTestFolder",
"parents": ["diPcXqabvgpX4tcZJZ-ms2Wg0bXf1Fem"]
}
I appreciate any assistance in cracking this Team folder creation.