Google Drive: Create Folder
Create a new folder in the specified folder on Google Drive
https://support.questetra.com/addons/googledrive-foldercreate/
2019-01-18 (C) Questetra, Inc. (MIT License)
Configs
  • C1: User who connects to Google Drive *
  • I1: Parent Folder ID(When empty,create in My Drive root) #{EL}
  • I2: Folder Name to create * #{EL}
  • O1: Data Item that will save Folder ID
  • O2: Data Item that will save web view url of Folder
Script
main();
function main(){
  let parentFolderId = configs.get("ParentFolderId");
  const folderName = configs.get("FolderName");
  if (folderName == "" ||folderName == null) {
    throw "Folder Name is blank";
  }
  
  const userId = configs.get("UserID");
  const quser = quserDao.findById(parseInt(userId));
  if (quser == null) {
    throw "User not found:" + userId;
  }
  engine.log("User Name:" + quser.getName());
  // get OAuth token
  let token;
  try{
    token = httpClient.getGoogleOAuth2Token(quser, "Drive");
  }catch(e){
    throw "This User has not connected with Google Drive."
  }
  const json = createFolder(token, parentFolderId, folderName);

  setData("FolderIdItem", json, "id");
  setData("WebViewUrlItem", json, "webViewLink");
}
  //save Data to process data
function setData(configName, json, property) {
  const item = configs.get(configName);
  if (item == null || item == "") {
    return;
  }
  const data = json[property];
  engine.setDataByNumber(item, data);
}
  //create folder on google drive
function createFolder(token, parentFolderId, name) {
  let jsonRes = {};
  //mime type of google drive folder
  jsonRes["mimeType"] = "application/vnd.google-apps.folder";
  if (parentFolderId !== "" && parentFolderId !== null){
    jsonRes["parents"] = [ parentFolderId + '' ];
  }
  jsonRes["name"] = name + '';
  
  const url = 'https://www.googleapis.com/drive/v3/files/';
  const response = httpClient.begin()
    .bearer(token)
    .queryParam("fields", "id, webViewLink")
    .queryParam("supportsTeamDrives","true")
    .body(JSON.stringify(jsonRes), "application/json; charset=UTF-8")
    .post(url);
  const status = response.getStatusCode();
  const responseTxt = response.getResponseAsString();
  if (status >= 300) {
    const error = "Failed to create \n status:" + status + "\n" + responseTxt
    throw error;
  }
  engine.log("status:" + status);
  engine.log(responseTxt);
  
  jsonRes = JSON.parse(responseTxt);
  return jsonRes;
}

Download

google-drive-folder-create.xml

Capture

Notes

  1. Users of C1 need to have configured connection with Google Drive at [Account Settings] > [Google Connectivity]

See also