Search code examples
c#asp.netasp.net-coregoogle-drive-apigoogle-drive-shared-drive

Drive API v3, C# - Folder Hierarchy


I've got an app that communicates with Drive (specifically TeamDrives) and I need to pull a hierarchical folder structure within a given TeamDrive.

I can list my available TeamDrives then get the files (filter: folders mime type) within them but there doesn't seem to be any parent info to each folder so my structure is seemingly 'flat'.

I get that on Drive a folder is just a label and that 'sub-folders' could be shared in several places so I will cater for that but I just want to be able to create the directory tree on my app.

e.g. structure as I want to show it in my app:

  • Team Drive Name
    • Main Folder
      • Sub Folder

My 'list' code:

var request = service.Files.List();
request.Corpora = "teamDrive";
request.IncludeTeamDriveItems = true;
request.SupportsTeamDrives = true;
request.OrderBy = "name";
request.PageSize = 100;

request.TeamDriveId = "[teamDriveId]";
request.Q = "mimeType='application/vnd.google-apps.folder'";

This gives me for a given 'teamDriveId':

  • Main Folder
  • Sub Folder

parent on the Sub Folder result is null.


Solution

  • You haven't specified which fields you want in the response, so you are getting the default which doesn't include parent information. Try adding request.Fields = "*". I'm not familiar with the library you are using, so I might have the wrong syntax - please double check.

    You might also find this useful In Google Apps Script, how would I get all subfolders and all subsubfolders and all subsubsub folders etc.?