Box: Search Folder
Searches for the folder in the specified folder on Box.
Configs: Common
  • Step Name
  • Note
Configs
  • C1: OAuth2 Setting *
  • C2: Parent Folder ID (Root Folder if blank)#{EL}
  • C3: Folder Name to search for *#{EL}
  • C4: Data Item that will save Folder ID
  • C5: Data Item that will save Folder URL
Script (click to open)

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.getObject( "FolderIdItem" );
  const urlData = configs.getObject( "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 oauth2 = configs.get("conf_OAuth2");

  const {folderId, folderUrl} = searchFolder(oauth2,parentFolderId,folderName);

  //set ID to Data Item
  if (idData !== null) {
    engine.setData(idData,folderId);
  }
 
  //set URL to Data Item  
  if (urlData !== null) {
    engine.setData(urlData, folderUrl);
  }
}

//search folder on box
function searchFolder(oauth2,parentFolderId,folderName){
  const url = `https://api.box.com/2.0/folders/${parentFolderId}/items`;
  let marker = "";
  const limit = httpClient.getRequestingLimit();
  for(let count = 0; count < limit; count++) {
    const response = httpClient.begin()
      .authSetting(oauth2)
      .queryParam("fields", "id,type,name")
      .queryParam("limit", "1000")
      .queryParam("usemarker","true")
      .queryParam("marker",marker)
      .get(url);
    const status = response.getStatusCode();
    const responseTxt = response.getResponseAsString();
    engine.log(`status: ${status}`);
    if (status >= 300) {
      engine.log(responseTxt);
      throw "Failed to search";
    }
    const json = JSON.parse(responseTxt)
    const items = json.entries;

    for(let i = 0; i < items.length; i++){
      if(items[i].type === "folder" && items[i].name === folderName){
        const url = `https://app.box.com/folder/${items[i].id}`;
        return {
          folderId : items[i].id,
          folderUrl : url
        };
      }
    }
    marker = json.next_marker;
    if(marker === undefined || marker === '' || marker === null) {
      throw `Folder ${folderName} doesn't exist.`;
    }
  }
  throw "Too many folders and files are in the specified folder";
}

Notes

  • Folder ID is contained in the URL. https:// {sub-domain}.app.box.com/folder/(Folder ID)
  • The refresh token for Box has the expiration. Use regularly to ensure that it does not exceed the expiration. (Box: Token & URL Expiration)

Capture

See also

Appendix

  • box-folder-search.xml (C) Questetra, Inc. (MIT License)
    • If you are using Professional, you can modify the contents of this file and use it as your own add-on

1 thought on “Box: Search Folder”

  1. Pingback: Utilising Box From Your Workflow – Questetra Support

Comments are closed.

%d bloggers like this: