is there a direct method to get file ID by giving a path (e.g. /some/folder/deep/inside/file.txt)? I know this can be done by recursively checking folder\'s contents, but a simp
An alternative to this would be to extract the target file/folder name from the path and search for it using the search API
like this: https://api.box.com/2.0/search?query=filename.txt
This gives back all the matching entries with their path_collections which provides the whole hierarchy for every entry. Something like this:
"path_collection": {
"total_count": 2,
"entries": [
{
"type": "folder",
"id": "0",
"sequence_id": null,
"etag": null,
"name": "All Files"
},
{
"type": "folder",
"id": "2988397987",
"sequence_id": "0",
"etag": "0",
"name": "dummy"
}
]
}
Path for this entry can be reverse engineered as /dummy/filename.txt
Just compare this path against the path you're looking for. If it matches, then that's the search result you're looking for. This is just to reduce the number of ReST calls you need to make to arrive at the result. Hope it makes sense.
We currently don't have support for this, but the feedback will definitely be considered as we continue building out the v2 API.
Here is my approach on how to get a folder id based on a path, without recursively going through the whole tree, this can be easily adapted for file as well. This is based on PHP and CURL, but it's very easy to use it in any other application as well:
//WE SET THE SEARCH FOLDER:
$search_folder="XXXX/YYYYY/ZZZZZ/MMMMM/AAAAA/BBBBB";
//WE NEED THE LAST BIT SO WE CAN DO A SEARCH FOR IT
$folder_structure=array_reverse (explode("/",$search_folder));
// We run a CURL (I'm assuming all the authentication and all other CURL parameters are already set!) to search for the last bit, if you want to search for a file rather than a folder, amend the search query accordingly
curl_setopt($curl, CURLOPT_URL, "https://api.box.com/2.0/search?query=".urlencode($folder_structure[0])."&type=folder");
// Let's make a cine array out of that response
$json=json_decode(curl_exec($curl),true);
$i=0;
$notthis=true;
// We need to loop trough the result, till either we find a matching element, either we are at the end of the array
while ($notthis && $i<count($json['entries'])) {
$result_info=$json['entries'][$i];
//The path of each search result is kept in a multidimensional array, so we just rebuild that array, ignoring the first element (that is Always the ROOT)
if ($search_folder == implode("/",array_slice(array_column($result_info['path_collection']['entries'],'name'),1))."/".$folder_structure[0])
{
$notthis=false;
$folder_id=$result_info['id'];
}
else
{
$i++;
}
}
if ($notthis) {echo "Path not found....";} else {echo "Folder id: $folder_id";}