Colour me stupid, but it took me a while to find out how to grab the documents in a particular folder (using the .NET library).

Google Docs API: get all spreadsheets/docs in a folder

It’s easy enough to get a list of spreadsheets for a user, or a list of folders, but there’s a trick to getting the contents of a folder.

The first part of the trick is to ignore the category. Firstly, because it’s removed in version 3.0 of the API, and secondly because it filters by name. This means that if you have 2 folders with the same name you’ll get dodgey results.

To do it right?

using Google.GData.Client;
using Google.GData.Extensions;
using Google.GData.Documents;

// snipped method declaration etc

var docService = new DocumentsService("company-app-version");
docService.setUserCredentials("username", "password");

var folderList = docService.Query(new FolderQuery());
var fLinks = folderList.Entries.Select(e =>
        // note how to get the document Id of the folder
        Id = DocumentsListQuery.DocumentId(e.Id.AbsoluteUri),
        Name = e.Title.Text

foreach (var folder in fLinks)
    Console.WriteLine("Folder {0}", folder.Name);

    var fileList = docService.Query(
        new SpreadsheetQuery()
            // setting the base address to the folder's URI restricts your results
            BaseAddress = DocumentsListQuery.folderBaseUri + folder.Id

    foreach (var file in fileList.Entries)
        Console.WriteLine(" - {0}", file.Title.Text);

This will give you a list of the files in all your folders. Note you can get a list of all files in all folders a lot easier than this. That’s not the point: use this technique when you don’t want to get the whole list and filter after the fact.

Pertinent points:

  • Line 15: getting the Id of the folder from its URI
  • Line 27: setting the base URI of the query to be the folder prefix and the folder’s Id.

I wish I’d found this quicker…

(Updated to add clarification about example, and a couple of comments in the code)


Leave a Reply

Your email is never shared.Required fields are marked *