17

I am trying to build a web UI for users to navigate his/her Google Drive and select one or more documents to be referenced later on in a website from a DB. I am currently building a web interface using PHP.

The problem I am facing is that I cant find a single function to get a list of files by folder Id.

I tried using:

$service->children->listChildren($rootFolderId)

…but that will only give the files reference ID of the files within the folder (the so called children resource item), which means I have to loop through those files and create a call for each one of them in order to get all the metadata I need for my UI.

Unfortunately..

$service->files->list()

..will list ALL my files with no filtering options by folder.

I would like to know if there is an efficient way of extracting a folder file list from a single call to the Drive server. I actually remember being able to perform this taks over the old Google DOC API.

Thank you very much for your help

Gwyn Howell
  • 5,365
  • 2
  • 31
  • 48
MightyMouse
  • 351
  • 1
  • 3
  • 12
  • see https://stackoverflow.com/questions/41741520/how-do-i-search-sub-folders-and-sub-sub-folders-in-google-drive – pinoyyid Jul 27 '18 at 10:28

4 Answers4

31

NB This answer uses the v2 API. New applications should write to the v3 API.

It is now possible to use the drive.files.list endpoint to list files belonging to a specific folder.

To do so, you can use the ?q= query parameter as follows:

GET https://www.googleapis.com/drive/v2/files?q="'<FOLDER_ID>' in parents"

In PHP, that would be

$service->files->list(array('q' => "'<FOLDER_ID>' in parents"));
pinoyyid
  • 21,499
  • 14
  • 64
  • 115
Alain
  • 6,044
  • 21
  • 27
  • 1
    This returns an error. If you test it here: https://developers.google.com/drive/v2/reference/files/list with a valid folder ID it returns '400 Bad Request' error. If you remove the 'in parents' bit then it returns the file object. – David Myers Jun 16 '13 at 20:08
  • 4
    After playing around with it some more, I was able to get this to work with one caveat: you must wrap the variable in single quotes in addition to the single quotes that are wrapping the search string. So it would look something like this: 'q' => "'$folderID' in parents" – David Myers Jun 17 '13 at 00:33
  • This one only returns first level children – sean Jan 28 '15 at 18:04
  • You can filter out trashed file by adding `trashed = false` to the query. The list of operators can be found here: https://developers.google.com/drive/web/search-parameters – Alain Jul 24 '15 at 17:27
3

Will the query parameter   <FOLDER_ID> in parents   list files and/or folders having such a <FOLDER_ID> in parents?
In my opinion,- this is the expected result. I ask this question because, in the above answer, one reads:

... to list files belonging to a specific folder.

and the documentation about search-parameters explains:

This finds all child folders for the parent folder whose ID is 1234567.

I'll add that a Folder-Only list could be returned by adding something like
and mimeType='application/vnd.google-apps.folder'
to the query string.

Pierre
  • 261
  • 3
  • 12
1

No need to use search queries. Simply make a call to GET https://www.googleapis.com/drive/v2/files/{folderId}/children

https://developers.google.com/drive/v2/reference/children/list

Radu Simionescu
  • 4,518
  • 1
  • 35
  • 34
1

Its easy now, don't worry.If you want to get all files and folder from specific FOLDER, then just copy and paste this code in your project and it will retrieve all the files and folder from the specific Folder. Note: You should have your own Folder ID

 List<File> result = new ArrayList<File>();
    Files.List request = null;

    try {
          request = mService.files().list();//plz replace your FOLDER ID in below line
          FileList files = request.setQ("'"+MyFoler_Id+"' in parents and trashed=false"").execute();
          result.addAll(files.getItems());          
          request.setPageToken(files.getNextPageToken());
        } 
      catch (IOException e)
      {
        System.out.println("An error occurred: " + e);
        request.setPageToken(null);
      }

//Print Out the Files and Folder Title which we have retrieved.

  for(File f:result)
  {
      System.out.println("My recvd data "+f.getTitle());
  }
Pir Fahim Shah
  • 10,505
  • 1
  • 82
  • 81