2

Question on listing the folders under shared drive.

I was able to get successful response while calling the API below by passing query parameters as:

  • includeTeamDriveItems=true
  • q=‘0AATe_aghhsdfvbdfg’ in parents and mimeType = ‘application/vnd.google-apps.folder’
  • supportsAllDrives=true

API: https://developers.google.com/drive/api/v3/reference/files/list?apix_params=%7B%22includeTeamDriveItems%22%3Atrue%2C%22q%22%3A%22%270AATe_aghhsdfvbdfg%27%20in%20parents%20and%20mimeType%20%3D%20%27application%2Fvnd.google-apps.folder%27%22%2C%22supportsAllDrives%22%3Atrue%7D

Successful Response:

{
 "kind": "drive#fileList",
 "incompleteSearch": false,
 "files": [
 {
  "kind": "drive#file",
  "id": "1E-c0rNCQMlQvXNUGTKSWdPHBOwwzjtcf",
  "name": "Integration",
  "mimeType": "application/vnd.google-apps.folder",
  "teamDriveId": "0AATe_aghhsdfvbdfg",
  "driveId": "0AATe_aghhsdfvbdfg"
 },
 {
  "kind": "drive#file",
  "id": "1QOMRSPuE1msJJmyr3yJOMZsBrn3nrtAx",
  "name": "Folder1",
  "mimeType": "application/vnd.google-apps.folder",
  "teamDriveId": "0AATe_aghhsdfvbdfg",
  "driveId": "0AATe_aghhsdfvbdfg"
 }
 ]
}

Question: From the response, it returns existing folders under the shared drive. Is it possible to also get all the sub folders under parent folders at once instead of having to pass parent folder ID under query parameters each time ? (e.g: To get all the sub folders under parent folder ‘Integration’)

  • Dilip
user9324800
  • 173
  • 2
  • 10
  • The folders just under the parent folder can be retrieved by the search query with one API call. But, all nested subfolders under the parent folder cannot be directly retrieved. In this case, it is required to prepare a script. Unfortunately, in the current stage, it seems that this is the current specification. I apologize for this. – Tanaike Jul 23 '20 at 23:15
  • Hi @Tanaike ! Your comment is correct, could you please formalise it into an answer so that it is easier to find for those with similar questions? Thanks ! – Mateo Randwolf Jul 24 '20 at 08:35
  • @Mateo Randwolf Thank you for your support. Yes. I posted it as an answer. – Tanaike Jul 24 '20 at 08:56

1 Answers1

2
  • The folders just under the parent folder can be retrieved by the search query with one API call like below. In this case, 'folderId' in parents is used as the search query.

      curl \
        'https://www.googleapis.com/drive/v3/files?corpora=drive&driveId=driveId&includeItemsFromAllDrives=true&q=%27folderId%27%20in%20parents&supportsAllDrives=true' \
        --header 'Authorization: Bearer [YOUR_ACCESS_TOKEN]' \
        --header 'Accept: application/json' \
        --compressed
    
    • And, when you want to retrieve the files under 2 folders, you can also use the following sample. In this case, 'folderIdA' in parents or 'folderIdB' in parents is used as the search query.

        curl \
          'https://www.googleapis.com/drive/v3/files?corpora=drive&driveId=driveId&includeItemsFromAllDrives=true&q=%27folderIdA%27%20in%20parents%20or%20%27folderIdB%27%20in%20parents&supportsAllDrives=true' \
          --header 'Authorization: Bearer [YOUR_ACCESS_TOKEN]' \
          --header 'Accept: application/json' \
          --compressed
      
    • From your question, I think that you have already been done above.

  • In the current stage, all nested subfolders under the parent folder cannot be directly retrieved.

    • In this case, it is required to prepare a script.

    • Unfortunately, in the current stage, it seems that this is the current specification at Google side.

Note:

  • In the most cases, includeItemsFromAllDrives=true and supportsAllDrives=true can be used. But I had the some cases that corpora=drive and driveId=### are also required. So when in your shared Drive, the files cannot be retrieved, please try to use this.

References:

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • Hi @ Tanaike thank you for the details. So, I tried to retrieve the files under 2 folders as per your instructions and it works fine. But if there so many folders (E.g: 200 parent folders) having nested subfolders under each parent folders, is it the same approach that needs to be followed ? – user9324800 Jul 27 '20 at 16:18
  • @user9324800 Thank you for replying. Yes. But in that case, there is the limitation of the query parameter like `'folderIdA' in parents or 'folderIdB' in parents ,,,,`. [Ref](https://stackoverflow.com/q/812925/7108653) I think that in this case, it's 2,048 bytes. So I think that in order to keep less than 2,048 bytes, when you want to retrieve all files in 200 folders, it is required to use the loop. – Tanaike Jul 27 '20 at 23:03
  • Thank you @ Tanaike – user9324800 Aug 16 '20 at 19:09
  • @user9324800 Thank you for replying. I'm glad your issue was resolved. Thank you, too. – Tanaike Aug 16 '20 at 22:37