Box: Search Folder

Box: Search Folder

Box: フォルダ検索

This item searches for a folder with a specific name directly under the specified folder on Box.

Auto Step icon
Basic Configs
Step Name
Note
Configs for this Auto Step
conf_OAuth2
C1: OAuth2 Setting *
ParentFolderId
C2: Parent Folder ID (Root Folder if blank)#{EL}
FolderName
C3: Folder Name to search for *#{EL}
FolderIdItem
C4: Data Item that will save Folder ID
WebViewUrlItem
C5: Data Item that will save Folder URL

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

Capture

See also

Script (click to open)
  • An XML file that contains the code below is available to download
    • 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 auto step

function main(){
  let parentFolderId = configs.get("ParentFolderId");
  if (parentFolderId === "" ||parentFolderId === null) {
    parentFolderId = "0";
  }
  const folderName = configs.get("FolderName");
  if (folderName === "" ||folderName === null) {
    throw new Error("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 new Error("Neither of Data Items to save result are set.");
  }
  const oauth2 = configs.getObject("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 new Error("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 new Error(`Folder ${folderName} doesn't exist.`);
    }
  }
  throw new Error("Too many folders and files are in the specified folder");
}

1 thought on “Box: Search Folder”

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

Comments are closed.

Scroll to Top

Discover more from Questetra Support

Subscribe now to keep reading and get access to the full archive.

Continue reading