- 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
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";
}
Pingback: Utilising Box From Your Workflow – Questetra Support