0

I got SubFolder ID with which I want to get its Parent Folder name in Google Drive. I tried searching on Google for long time but didn't get anywhere. I tried with the code (see below) but its not working. Please can anyone help?

public string GetFolderName(DriveService service, string folderID)
    {
        FilesResource.ListRequest request = service.Files.List();
        request.Q = "'" + folderID + "' mimeType = 'application/vnd.google-apps.folder' and title = 'My_Folder_name'";
        FileList files = request.Execute();
        return files.Items[0].Title;
    }
kami
  • 259
  • 4
  • 13

2 Answers2

0

Use https://developers.google.com/drive/v3/reference/files/get to get the metadata for folderId. Include fields=parents which will return a list of all the subfolder's parents (there can be many - see How do I search sub-folders and sub-sub-folders in Google Drive?).

Once you have the parent(s) id's, you can use the same get with fields=name to get their names.

Community
  • 1
  • 1
pinoyyid
  • 21,499
  • 14
  • 64
  • 115
0

Try using Parents: list

Lists a file's parents.

This will return :

{
  "kind": "drive#parentList",
  "etag": etag,
  "selfLink": string,
  "items": [
    parents Resource
  ]
}

Once you get the fileID of the Parent, you can now request to get the file name of the folder. As stated in this SO post, parents resource doesn't contain the title of the fileID and will have to create a multiple request.

Hope this helps.

Community
  • 1
  • 1
Mr.Rebot
  • 6,703
  • 2
  • 16
  • 91