Comprehensive Guide: Listing All Files on Google Drive with Ease 11


Google Drive API V3
Google Drive API V3

I recently ran across a question on Stack overflow. The question was quite simple how to retrieve the folder values of from Google drive and display them in a directory list using C# and the Google .Net client library.

I have used a PageStreamer in the ListAll method in the event that there are more then 1000 files in the directory this will ensure that we get them all back. PageStreaming is much easier then having to deal with the nextPageToken yourself.

After we have all of the results then PrettyPrint is running recursively to request all of the files from within any directories.

Conclusion

By using PageStreamer you can retrieve all of the rows for your request rather then having to worry about the nextPageToken yourself.

Note: I am not responsible for the usage of your quota if you print everything 🙂


About Linda Lawton

My name is Linda Lawton I have more than 20 years experience working as an application developer and a database expert. I have also been working with Google APIs since 2012 and I have been contributing to the Google .Net client library since 2013. In 2013 I became a a Google Developer Experts for Google Analytics.

Leave a Reply to Linda Lawton Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

11 thoughts on “Comprehensive Guide: Listing All Files on Google Drive with Ease

  • Marcel Talamini

    Hi Linda,
    Thanks for all your examples and explanations. It helped me a lot.

    I have a question.
    I have up to 3000 files on my Drive (service account).

    I have to show some kind of Explorer with the files and folders.
    What would you recommend?
    Read all files at startup and populate the explorer with the result. or
    Build the explorer on demand from root. Which could result in more request.

    NB.
    Do I need to build an Exponential Backoff, when using the “PageStreamer”?

    Thx

    • Linda Lawton Post author

      Do I need to build an Exponential Backoff, when using the “PageStreamer”?

      The client library has built in support for back-off. You shouldn’t need to worry about it, we have it taken care of.

      Explorer with the files and folders.

      There is a few things you need to consider with this. The first being memory. Whatever you return there needs to be enough memory in your system to hold the object. Currently you only have 3000 files which shouldnt be a problem just requesting them all in the start and using them as you go. If you start to get more files and things begin to slow down you may have to revisit it.

  • Srusti Thakkar

    Hello,
    I want to list files which are owned by that particular user only, not shared with user. What scope I have to set?

  • Atanu

    I am using google drive api, it is working fine when I am using locally, but when I host in my local IIS, then it is not working,
    protected void btnList_Click(object sender, EventArgs e)
    {
    String CLIENT_ID = “”;
    String CLIENT_SECRET = “My_CLIENT_SECRET”;
    string[] scopes = new string[] { DriveService.Scope.Drive,
    DriveService.Scope.DriveFile};
    string jsonpath = Server.MapPath(“~/App_Data/”);
    //////// here is where we Request the user to give us access, or use the Refresh Token that was previously stored in %AppData%
    //IAuthorizationCodeFlow credential = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
    //{
    // ClientSecrets = new ClientSecrets
    // {
    // ClientId = CLIENT_ID,
    // ClientSecret = CLIENT_SECRET
    // },
    // Scopes = new[] { DriveService.Scope.Drive },
    // DataStore = new FileDataStore(jsonpath)
    //});

    UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync
    (new ClientSecrets
    {
    ClientId = CLIENT_ID,
    ClientSecret = CLIENT_SECRET
    }
    , scopes
    , Environment.UserName
    , CancellationToken.None
    , new FileDataStore(jsonpath)).Result;
    DriveService service = new DriveService(new BaseClientService.Initializer()
    {
    HttpClientInitializer = credential,
    ApplicationName = “Drive API Quickstart”,
    });
    string emlid = txtMailId.Text.Trim();
    string Q = “‘” + emlid + “‘ in writers”;
    IList _Files = new List();
    _Files = GetFiles(service, Q);

    List objGDriveFileList = new List();
    foreach (File item in _Files)
    {
    GDriveFile objGDriveFile = new GDriveFile();
    if (item.MimeType == “application/vnd.google-apps.document”)
    {
    objGDriveFile.FileName = item.Title;
    objGDriveFile.FileLink = item.AlternateLink;
    objGDriveFileList.Add(objGDriveFile);
    }
    }
    GvGDriveFileList.DataSource = objGDriveFileList;
    GvGDriveFileList.DataBind();
    }

    public static IList GetFiles(DriveService service, string search)
    {

    IList Files = new List();

    try
    {
    //List all of the files and directories for the current user.
    // Documentation: https://developers.google.com/drive/v2/reference/files/list
    FilesResource.ListRequest list = service.Files.List();
    list.MaxResults = 1000;
    if (search != null)
    {
    list.Q = search;
    }
    FileList filesFeed = new FileList();
    filesFeed = list.Execute();

    //// Loop through until we arrive at an empty page
    while (filesFeed.Items != null)
    {
    // Adding each item to the list.
    foreach (File item in filesFeed.Items)
    {
    Files.Add(item);
    }

    // We will know we are on the last page when the next page token is
    // null.
    // If this is the case, break.
    if (filesFeed.NextPageToken == null)
    {
    break;
    }

    // Prepare the next page of results
    list.PageToken = filesFeed.NextPageToken;

    // Execute and process the next page request
    filesFeed = list.Execute();
    }
    }
    catch (Exception ex)
    {
    // In the event there is an error with the request.
    // Console.WriteLine(ex.Message);
    }
    return Files;
    }

    public class GDriveFile
    {
    public string FileName { get; set; }
    public string FileLink { get; set; }
    }

    • Linda Lawton Post author

      Google Picker is a “File Open” dialog for the information stored in Google servers.

      I am not a web developer I am a backend developer. This is not going to work for me on a server running a background process. That and this tutorial is for the Google Drive API. The google picker APIs a separate API which if i had it would be part of a different tutorial series on Google picker.

  • Ravindra Reddy Gujjula

    Hi Linda,
    Is there any way to get Files and Folders from Computers Folder instead of my drive.
    I tried your script it shows only the files and folders present in the my drive.
    I want all files and folders which are synced from all different computers in computers menu.
    Why I am Using this because I want to compare system files with google drive synced files using c# so that i can check which are files uploaded.
    Can you please help me out. I am new to coding