box-apiboxboxapiv2

How to get the BoxFileVersion object of the previous version by using the version number and fileID using Box-SDK?


I can get the current version of the file using the code below?

 BoxFile file = new BoxFile(api,fileId);
 BoxFile.Info info  = file.getInfo("version_number","file_version");
 info.getVersionNumber(); // current version No.

Now I wanted to fetch the BoxFileVersion Object for the Given Version Number, In below code i tried to get the previous version of the file, but i am unable to get the VERSION NUMBER for the specific versions

  Collection<BoxFileVersion> versions = file.getVersions();   // Fetching the Previous Version of the Files
    if(versions.size() != 0){     // If there is no Previous Versions
         for(BoxFileVersion bfv : versions){
              if(bfv.getTrashedAt() == null){
                     bfv.promote();
                     boxFileVersion.delete();
                     System.out.println("Deleted Version ID : "+boxFileVersion.getVersionID());
                     break;
               }
          }
     }
     else{
            file.delete();   // delete the file if no previous version exist
      }

Solution

  • So I tested out your code without deleting previous versions and it seems to work.

    BoxFile file = new BoxFile(userApi, "xxxxxx");
            System.out.println("file current version: " + file.getInfo().getVersion().getVersionID());
    
            Collection<BoxFileVersion> versions = file.getVersions();   // Fetching the Previous Version of the Files
    
            int versionIndex = versions.size();
            if (versions.size() != 0) {     // If there is no Previous Versions
                for (BoxFileVersion bfv : versions) {
                    if (versionIndex == versions.size())  // the first one is the previous version
                    {
                        bfv.promote();
                        bfv.delete();
                        System.out.println("Deleted Version ID : "+ bfv.getVersionID());
                    }
                    System.out.println("bfv: [" + versionIndex-- + "] " + bfv.getVersionID() + " " + bfv.getCreatedAt());
                }
            }
    

    and there doesn't seem to be a version number but version ids. So I guess the version #'s are just the position in the version array.

    and here's the output:

    file current version: xxxx42182218
    Deleted Version ID : xxxx42064367
    bfv: [4] xxxx42064367 Tue May 09 16:43:54 PDT 2017
    bfv: [3] xxxx32054815 Tue May 09 16:28:50 PDT 2017
    bfv: [2] xxxx28578550 Tue May 09 16:19:47 PDT 2017
    bfv: [1] xxxx28578201 Tue May 09 16:19:41 PDT 2017
    file current version: xxxx47266430