Box: Search Folder
Search for the folder in the specified folder on Box
2019-08-15 (C) Questetra, Inc. (MIT License)
Configs
  • C1: OAuth2 Setting Name *
  • C2: Parent Folder ID (Root Folder if blank) #{EL}
  • C3: Folder Name to search for * #{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");
  if (parentFolderId === "" ||parentFolderId === null) {
    parentFolderId = "0";
  }
  const folderName = configs.get("FolderName");
  if (folderName === "" ||folderName === null) {
    throw "Folder Name is blank";
  }
  //get C4&C5
  const idData = configs.get("FolderIdItem");
  const urlData = configs.get("WebViewUrlItem");
  //If neither C4 nor C5 are set,throw error
  if((idData === null || idData === "") && (urlData === null || urlData === "")){
    throw "Neither of Data Items to save result are set.";
  }
  const oauth = configs.get("conf_OAuth2");

  // get OAuth token
  let token;
  token = httpClient.getOAuth2Token( oauth );
  //const folderNameRep = folderName.replace(/['\\]/g,"\\$&");
  //const q = "mimeType = 'application/vnd.google-apps.folder' and trashed = false and name = '" + folderNameRep + "' and '" + parentFolderId + "' in parents";
  const json = searchFolder(token,parentFolderId);
  
  let folderIdList = "";
  let folderUrlList = "";
  let folderCount = 0;
  const folderNum = json.length;
  for(let i = 0; i < folderNum; i++){
    if(json[i].type === "folder" && json[i].name === folderName){
      folderCount++;
      if (folderCount > 1){
        folderIdList += "\n";
        folderUrlList += "\n";
      }
      folderIdList += json[i].id;
      folderUrlList += "https://app.box.com/folder/" + json[i].id;
    }
  }
  if (folderIdList === ""){
    throw "Folder " + folderName + " doesn't exist.";
  }
  //set ID to Data Item
  if (idData !== null && idData !== "") {
    const idDataDef = engine.findDataDefinitionByNumber(idData);
    //Multiple Judge
    if(idDataDef.matchDataType("STRING_TEXTFIELD") && folderCount > 1){
      throw "Multiple folders are found.Can't set data to single-line string Data Item."
    }
    engine.setDataByNumber(idData,folderIdList);
  }
  //set URL to Data Item
  if (urlData !== null && urlData !== "") {
    const urlDataDef = engine.findDataDefinitionByNumber(urlData);
    //Multiple Judge
    if(urlDataDef.matchDataType("STRING_TEXTFIELD") && folderCount > 1){
      throw "Multiple folders are found.Can't set data to single-line string Data Item."
    }
    engine.setDataByNumber(urlData,folderUrlList);
  }
}
//search folder on box
function searchFolder(token,parentFolderId){
  const url = 'https://api.box.com/2.0/folders/' + parentFolderId +  '/items';
  let marker = "";
  let jsonRes = [];
  let json;
  const limit = httpClient.getRequestingLimit();
  for(let count = 0; count < limit; count++) {
    const response = httpClient.begin()
      .bearer(token)
      .queryParam("fields", "id,type,name")
      .queryParam("limit", 1000)
      .queryParam("usemarker",true)
      .queryParam("marker",marker)
      .get(url);
    const status = response.getStatusCode();
    const responseTxt = response.getResponseAsString() + "\n";
    if (status >= 300) {
      const error = "Failed to search \n status:" + status + "\n" + responseTxt;
      throw error;
    }
    json = JSON.parse(responseTxt)
    jsonRes = jsonRes.concat(json.entries);
    engine.log("status:" + status + "\n" + responseTxt);
    marker = json['next_marker'];
    if(marker === undefined || marker === '' || marker === null) {
      return jsonRes;
    }
    
  }
  throw "Too many folders and files are in the specified folder";
}

Download

Capture

Notes

  1. Folder ID is contained in the URL. https://{sub-domain}.app.box.com/folder/(Folder ID)
  2. 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)