list of folders in main folder

New post

Comments

9 comments

  • Murtza

     Have you granted access for your app in the Enterprise Admin console

    0
    Comment actions Permalink
  • Box Product Support

    Hi Murtza,

    Thank you for your response.

    I just started with box, so not very good in it.

    Basically what I need is to upload a file to a folder (choose folder by name not by ID) using PHP (curl).

    I do not know if I going correct path, but what I done so far is went through

    Build a Box Integration tutorials.

    I created box application.

    Found how to get developers token, refresh token.

     

    I was able to create folder

     

    $parent = array();
    		$parent['id'] = '0';
    		$params = array();
    		$params['name'] = 'Testfolder';
    		$params['parent'] = $parent;
    
    		$params = json_encode($params);
    
    		$ch = curl_init();
    		curl_setopt($ch, CURLOPT_URL, "https://api.box.com/2.0/folders");
    		curl_setopt($ch, CURLOPT_HEADER, false); 
    		curl_setopt($ch, CURLOPT_POST, true);
    		curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
    		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    		curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
    		curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json',                                                                                
    		    'Content-Length: ' . strlen($params), 'Authorization: Bearer '.$token));
    		$result = curl_exec($ch);
    		curl_close($ch);
    	
    		print_r($result);

     

    , upload file to main folder or created folder knowing folder's ID.

    $url = "https://upload.box.com/api/2.0/files/content";
    	
    	 $json = json_encode(array(
                                    'name' => 'meter.xlsx', 
                                    'parent' => array('id' =>'0')// 0 - uploads to main folder
                                ));
             $fields = array(
                          'attributes' => $json,
                          'file'=> new CurlFile('meter.xlsx','application/octet-stream','meter.xlsx')
                      );
    
           
                $ch = curl_init();
                curl_setopt($ch,CURLOPT_URL, $url);
    	    
       	  
                curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                    'Authorization: Bearer '.$token
                ));
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    	    curl_setopt($ch, CURLOPT_POST, true);
                curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
                $response = curl_exec($ch);
                curl_close($ch);
            
    
            print_r($response);

    but how to upload a file to a folder by name not by ID that is a problem.

    The idea was to Get Folder's Items of main folder, then match ID with name in json response received.

     

     

    0
    Comment actions Permalink
  • Box Product Support

    I'm using the Java SDK and I have the same problem with "folder.getInfo()".

     

    Using the sample code provided (as below), I also get error 405.

     

        BoxFolder folder = new BoxFolder(api, "candy");
        BoxFolder.Info info = folder.getInfo();

     

    I get this error too for "folder.delete()"

     

    In both cases when I debug it, it fails when the URL request is sent.

    0
    Comment actions Permalink
  • Box Product Support

    Yes.

    0
    Comment actions Permalink
  • Box Product Support

    Could this be relevant to your problem?

     

    http://stackoverflow.com/questions/13977266/suddenly-getting-405-method-not-allowed-during-post-to-create-folder-in-box-api

    0
    Comment actions Permalink
  • kendomen

    The 2nd parameter to BoxFolder in box-java-sdk expects an ID. 

           

    BoxFolder boxfolder = new BoxFolder(api, "removed for privacy7");  // api, ID
    BoxFolder.Info boxFolderInfo = boxfolder.getInfo();
    System.out.println(boxFolderInfo.getName());

     

    0
    Comment actions Permalink
  • Box Product Support

    Oh thanks Ken.

     

    How do you get the ID of a folder if you only know its name?

     

    I'm going to guess that it's by iterating through all items and looking for the name (?)

     

             for (BoxItem.Info itemInfo : parentFolder)

     

     

    PeterD

    0
    Comment actions Permalink
  • kendomen

    I think so.  Here's 2 ways I search for something...

    Given a BoxFolder, you can search like you mention above...

     

    for (BoxItem.Info itemInfo : folder) {
      if (itemInfo instanceof BoxFile.Info) {
         BoxFile.Info fileInfo = (BoxFile.Info) itemInfo;
         if (fileName.equals(fileInfo.getName())) {
            fileExists = true;
            fileId = fileInfo.getID();
         }
      }
    }

    Or use BoxSearch:

    BoxSearch boxSearch = new BoxSearch(api);
    
    BoxSearchParameters boxSearchParameters = new BoxSearchParameters("ken");  // search parameter name=ken
    boxSearchParameters.setType("folder");                                     // only return folders
    
    List parentFolderId = new ArrayList<>();
    parentFolderId.add("0"); // eg.  All Files
    boxSearchParameters.setAncestorFolderIds(parentFolderId);
    
    PartialCollection results = boxSearch.searchRange(0, 10, boxSearchParameters);
    for (Iterator iterator = results.iterator(); iterator.hasNext(); ) {
       BoxItem.Info info = iterator.next();
       System.out.println(info.getParent().getName() + "-" + info.getName() + " [" + info.getID() + "]");           
    }

     

     

     

    0
    Comment actions Permalink
  • Box Product Support

    Thanks Ken, 

     

    this was very helpful.

     

    regards

    PeterD

    0
    Comment actions Permalink

Please sign in to leave a comment.