Sean O'Neil
Sean O'Neil

Reputation: 1241

Google Drive v3 - How to Check if Folder is in Root?

I want to be able to determine if a folder is in the root folder. Documentation says use "root" in place of the id for queries, but that doesn't help me here. If you look at the 'parents' of any folder in the root folder, it still shows an actual id, not the literal "root". How can one obtain this id?

I see there was a method in 'About' to get the root id in v2, but it's not in v3.

I'm using .NET but a solution in any language should be fine. Thanks.

Upvotes: 2

Views: 1466

Answers (1)

Tanaike
Tanaike

Reputation: 201583

How about this answer? From your question, I thought that retrieving the folders just under the root folder might become your solution. If my understanding is correct, how about using files.list method of Drive API? In this case, I think that there are 2 patterns.

Pattern 1:

In this pattern, all folders just under the root folder are retrieved. You can check the folder from this list.

  • 'root' in parents and mimeType = 'application/vnd.google-apps.folder' is used as the query for searching files.

Endpoint:

GET https://www.googleapis.com/drive/v3/files?q='root'+in+parents+and+mimeType+%3D+'application%2Fvnd.google-apps.folder'

Sample script:

FilesResource.ListRequest listRequest = service.Files.List();
listRequest.Q = "'root' in parents and mimeType = 'application/vnd.google-apps.folder'";
var files = listRequest.Execute();

Pattern 2:

In this pattern, using the folder name, the folders just under the root folder are retrieved. You can know whether the folder is in the root folder from the folder name. If the folder of the searched folder name is found in the root folder, the array of files.files of returned value has elements.

  • 'root' in parents and mimeType = 'application/vnd.google-apps.folder' and name = 'folderName' is used as the query for searching files.

Endpoint:

GET https://www.googleapis.com/drive/v3/files?q='root'+in+parents+and+mimeType+%3D+'application%2Fvnd.google-apps.folder'+and+name+%3D+'folderName'

Sample script:

FilesResource.ListRequest listRequest = service.Files.List();
listRequest.Q = "'root' in parents and mimeType = 'application/vnd.google-apps.folder' and name = 'folderName'";
var files = listRequest.Execute();

References:

If I misunderstood your question and this was not the result you want, I apologize.

Upvotes: 4

Related Questions