Box: Create Folder

Box: Create Folder
Create a new folder in the specified folder on Box
2019-07-19 (C) Questetra, Inc. (MIT License)
Configs
  • C1: OAuth2 Setting Name *
  • C2: Parent Folder ID (Root Folder if blank) #{EL}
  • C3: Folder Name to create * #{EL}
  • C4: String type data Item that will save Folder ID
  • C5: String type data Item that will save Folder URL
Script
main();
function main(){
  let parentFolderId = configs.get("ParentFolderId");
  const folderName = configs.get("FolderName");
  const reg = new RegExp('[/\]');
  if (folderName == "" ||folderName == null) {
    throw "Folder Name is blank";
  }else if(folderName.length>255){
    throw"Folder Name shoule be less than 256 characters";
  }else if(folderName.search(reg) !== -1|| folderName.slice(-1) == " " || folderName == "." || folderName == ".."){
    throw "Invalid Folder Name";
  }
  // get OAuth token
  const token = httpClient.getOAuth2Token(configs.get("conf_OAuth2"));
  const json = createFolder(token, parentFolderId, folderName);
  const IdData = configs.get("FolderIdItem");
  const UrlData = configs.get("WebViewUrlItem");
  if (IdData !== null && IdData !== "") {
      engine.setDataByNumber(IdData, json["id"]);
  }
  if (UrlData !== null && UrlData !== "") {
      engine.setDataByNumber(UrlData, "https://app.box.com/folder/" + json["id"]);
  }
}
  //create folder on google drive
function createFolder(token, parentFolderId, name) {
  let jsonRes = {};
  //mime type of google drive folder
  if (parentFolderId !== "" && parentFolderId !== null){
    jsonRes["parent"] = {"id": parentFolderId + '' };
  }else{
    jsonRes["parent"]= {"id": 0 + '' }
  }
  jsonRes["name"] = name + '';
  
  const url = 'https://api.box.com/2.0/folders';
  const response = httpClient.begin()
    .bearer(token)
    .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

Capture

Notes

  1. Folder ID is contained in the URL. https://{sub-domain}.app.box.com/folder/(Folder ID)
  2. If there are foldername conflicts, it results in an error.
  3. The refresh token for Box has the expiration. Use regularly to ensure that it does not exceed the expiration. (60 days, as of July 2019. https://box-content.readme.io/docs/oauth-20)
%d bloggers like this: