2

I am using the Google Drive API libraries in my Play Framework application.

I am trying to search a Team Drive, check if a folder exists, and create the folder if is does not exist. I was using the FileList object before the TeamDriveList, but using the FileList was constantly creating the folder in the Team Drive even if it existed - it would never find the existing folder. It would duplicate the folder over and over.

So, after reading a few posts:

Iterate over Team Drive files Google Drive API JAVA

Google Drive API - Creates Duplicate Folders

It seemed like I needed to switch over to the TeamDriveList.

I am using this example in Google's documentation: https://developers.google.com/drive/v3/web/manage-teamdrives#managing_team_drives_for_domain_administrators

As you can see, it uses setQ to perform a search:

TeamDriveList result = driveService.teamdrives().list()
      .setQ("organizerCount = 0")
      .setFields("nextPageToken, teamDrives(id, name)")
      .setUseDomainAdminAccess(true)
      .setPageToken(pageToken)
      .execute();

However, in my code, when I try to compile, it throws an error that setQ does not exist.

Here is my code:

public static String getSubfolderID(Drive service, String parentFolderID, String folderKeyToGet) {
    // We need to see if the folder exists based on the ID...
    String folderID = "";
    Boolean foundFolder = false;
    TeamDriveList result = null;
    File newFolder = null;

    // Set the drive query...
    String driveQuery = "mimeType='application/vnd.google-apps.folder' and '" + parentFolderID
            + "' in parents and name contains '" + folderKeyToGet
            + "' and trashed=false";

    try {
        result = service.teamdrives().list()
                .setQ(driveQuery)
                .execute();
    } catch (IOException e) {
        e.printStackTrace();
    }
    for (TeamDrive folder : result.getTeamDrives()) {
        System.out.printf("Found folder: %s (%s)\n", folder.getName(), folder.getId());
        foundFolder = true;
        folderID = folder.getId();
    }
    if (foundFolder != true) {
        // Need to create the folder...
        File fileMetadata = new File();
        fileMetadata.setName(folderKeyToGet);
        fileMetadata.setTeamDriveId(parentFolderID);
        fileMetadata.set("supportsTeamDrives", true);
        fileMetadata.setMimeType("application/vnd.google-apps.folder");
        fileMetadata.setParents(Collections.singletonList(parentFolderID));

        try {
            newFolder = service.files().create(fileMetadata)
                    .setSupportsTeamDrives(true)
                    .setFields("id, parents")
                    .execute();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // Send back the folder ID...
        folderID = newFolder.getId();
        System.out.println("Folder ID: " + newFolder.getId());
    }

    return folderID;
}

The line of code in question is:

    result = service.teamdrives().list()
            .setQ(driveQuery)
            .execute();

If this does not exist, how do I search, using this query:

String driveQuery = "mimeType='application/vnd.google-apps.folder' and '" + parentFolderID
        + "' in parents and name contains '" + folderKeyToGet
        + "' and trashed=false";

for the folder I am looking for and make sure I find the folder so not to cause duplicates?

Appreciate the help.

Dan
  • 940
  • 2
  • 14
  • 42

0 Answers0