0

I am trying to get folder id by folder name by using google drive api. Is there anyway i could get Folder id of a folder in drive by passing its name. I am developing this in .net using c#.

I have tried the code provided on the below link Google DRIVE API V3 - Get a folder id with name

But it was not clearly understood how to execute that.

Any help on this appreciated !

XamDev
  • 3,377
  • 12
  • 58
  • 97
  • @MindSwipe Agree, But after that which method to execute that's not clear – XamDev Mar 08 '19 at 12:01
  • Check out [this](https://developers.google.com/drive/api/v3/search-parameters) from google itself with sample code for .NET – MindSwipe Mar 08 '19 at 12:04
  • @MindSwipe Yeah but how to pass the metadata in that case – XamDev Mar 08 '19 at 12:11
  • Which metadata? – MindSwipe Mar 08 '19 at 12:12
  • name and mimetype which we pass in metadata, like shown in the example – XamDev Mar 08 '19 at 12:13
  • Instead of `request.Q = "mimeType='image/jpeg'` replace it with `request.Q = "mimeType='application/vnd.google-apps.folder'"` and (I'm not sure from here on out) add the name field to the `Fields` Property like so: `request.Fields = "nextPageToken, **name=your-name-here**, files(id, name)";`, you can always use string interpolation ($"") to set your name to whatever – MindSwipe Mar 08 '19 at 12:23
  • Yeah.. I have already tried till this point. But it give error as invalid field selection for name field – XamDev Mar 08 '19 at 12:40

1 Answers1

2

@XamDev Let's say I have a folder "test" on my google drive. I would like to find this folder by giving Google drive API, the name of the folder and restrict the results only to folders. This is how you can do it (It's working in my case):

        FilesResource.ListRequest listRequest = service.Files.List();
        listRequest.PageSize = 10;
        listRequest.Q = "mimeType = 'application/vnd.google-apps.folder' and name = 'test'";
        listRequest.Fields = "nextPageToken, files(id, name)";

        IList<Google.Apis.Drive.v3.Data.File> files = listRequest.Execute()
        .Files;
        if (files != null && files.Count > 0)
        {
            foreach (var file in files)
            {   //My TextBlock(WPF)
                ListedFiles.Text = $"{file.Name}, {file.Id} \n"; 
            }
        }

The part of the source code comes from: https://developers.google.com/drive/api/v3/quickstart/dotnet and the parameters that you can search for: https://developers.google.com/drive/api/v3/search-parameters

GoodOldGuy
  • 172
  • 4
  • 8