restgoogle-drive-api

Can you add a new version of an existing Google Doc using the Drive REST API?


Wondering if anyone has actually added new versions of docs using the Drive REST API, automating the manual process of right clicking on a Doc in Drive, going to Manage Versions, Upload New Version? I'm looking to add new versions of a PDF in my case.

The closest I have come to finding the answer via the Drive REST API documentation is here, using File:update:

https://developers.google.com/drive/v2/reference/files/update

But I don't see in there if it would actually add a new version or not, leaving historical versions in tact.

Thanks in advance for any input or advice,

Dave


Solution

  • The answer is YES, anytime you perform update on a file/folder,

     Drive mGOOSvc;
     ...
    /****************************************************************
     * update file in GOODrive,  see https://youtu.be/r2dr8_Mxr2M 
     * @param resId  file  id
     * @param titl  new file name (optional)
     * @param mime  new mime type (optional, "application/vnd.google-apps.folder" indicates folder)
     * @param file  new file content (optional)
     * @return      file id  / null on fail
     */
    String update(String resId, String titl, String mime, String desc, java.io.File file){
      com.google.api.services.drive.model.File gFl = null;
      if (mGOOSvc != null  && resId != null) try {
        com.google.api.services.drive.model.File meta = new File();
        if (titl != null) meta.setTitle(titl);
        if (mime != null) meta.setMimeType(mime);
        if (desc != null) meta.setDescription(desc);
        
        if (file == null)
          gFl = mGOOSvc.files().patch(resId, meta).execute();
        else
          gFl = mGOOSvc.files().update(resId, meta, new FileContent(mime, file)).execute();
        
        } catch (Exception e) { e.printStackTrace();}
      return gFl == null ? null : gFl.getId();
    }
    

    you will see a new version of the object using drive.google.com -> Manage versions...

    It works in the 'update' branch, I did not test the 'patch' branch, though.

    Good Luck