Contents

Abstract

This document is intended for Apex Developers who want to write Force.com Apps that can interact with Google's Documents List Data API.

This document assumes that you understand the general ideas behind the Google Data APIs protocol.

For additional Google Documents List Data API reference information, see the Google Documents List Data API Reference Guide.

Getting started

Set up your Force.com development environment.

The Force.com platform is the most scalable, secure and popular on-demand platform in the world today. You can develop and instantaneously deploy applications without any infrastructure - 100% on-demand. If you don't already have one, you can get a free Developer Edition environment by joining developer.force.com.

Install the latest version of the Force.com IDE

The Force.com IDE allows developers a full-featured, Eclipse-based coding environment, with capabilities like code completion, version control,collaborative development, and project sharing.

Create a Google Account

Google Documents are associated with Google Accounts, so a Google Account will be required in order to proceed. To view your documents without using the Documents List Data API, you can log in to Gmail and click the Documents link.

Check out the source code

Check out the Force.com Toolkit for Google Data APIs from the Google repository.

Apex Code Examples

The following examples show how to send Google Documents List Data API requests from Apex Code.

Authenticating to the Documents service

Create the Apex object that will handle the connection to Google Documents List Data API:

DocumentService service = new DocumentService();  
service.setAuthSubToken(sessionAuthToken);  

Retrieving a list of documents

You can get a feed containing a list of the currently authenticated user's spreadsheets by sending the following request from Apex code:

GoogleData documents = service.getDocuments(); // list all documents
documents.dump();

The result is a feed that lists all of that user's documents; each entry in the feed represents a document (spreadsheet or word processor document) associated with the user.

Entries in the Documents List feed might look something like the following (dump output from above):

feed-> {gd=http://schemas.google.com/g/2005, openSearch=http://a9.com/-/spec/opensearchrss/1.0/, xmlns=http://www.w3.org/2005/Atom}
   id->http://docs.google.com/feeds/documents/private/full {}
   updated->2008-06-18T21:37:18.866Z {}
   title->Available Documents - sforcedemos@example.com {type=text}
   link-> {href=http://docs.google.com, rel=alternate, type=text/html}
   link-> {href=http://docs.google.com/feeds/documents/private/full, rel=http://schemas.google.com/g/2005#feed, type=application/atom+xml}
   link-> {href=http://docs.google.com/feeds/documents/private/full, rel=http://schemas.google.com/g/2005#post, type=application/atom+xml}
   link-> {href=http://docs.google.com/feeds/documents/private/full/-/document, rel=self, type=application/atom+xml}
   author-> {}
      name->sforcedemo {}
      email->sforcedemos@example.com {}
   openSearch:totalResults->7 {}
   openSearch:startIndex->1 {}
   entry-> {}
      id->http://docs.google.com/feeds/documents/private/full/document%3Adocument_id {}
      updated->2008-06-09T03:14:32.753Z {}
      category-> {label=document, scheme=http://schemas.google.com/g/2005#kind, term=http://schemas.google.com/docs/2007#document}
      title->three document {type=text}
      content-> {src=http://docs.google.com/RawDocContents?action=fetch&justBody=false&revision=_latest&editMode=false&docID=dfz6hg7s_6fh5gxfdn, type=text/html}
      link-> {href=http://docs.google.com/Doc?id=dfz6hg7s_6fh5gxfdn, rel=alternate, type=text/html}
      link-> {href=http://docs.google.com/feeds/documents/private/full/document%3Adocument_id, rel=self, type=application/atom+xml}
      link-> {href=http://docs.google.com/feeds/documents/private/full/document%3Adocument_id/fh8hqc35, rel=edit, type=application/atom+xml}
      author-> {}
         name->sforcedemo {}
         email->sforcedemos@example.com {}
      gd:feedLink-> {href=http://docs.google.com/feeds/acl/private/full/document%3Adocument_id, rel=http://schemas.google.com/acl/2007#accessControlList} 

Notice that their types can be distinguished by the category element with the http://schemas.google.com/g/2005#kind scheme. The word processor document id has been replaced with document_id.

Uploading documents

Documents are uploaded to the server via an HTTP POST optionally using MIME multipart encoding to combine the the document contents with a Atom entry describing the document. Any combination of Atom entry and an HTTP Slug header can be used to specify the title of the uploaded document, but the following precedence will determine the final document name:

  1. The Atom entry <atom:title/> if present
  2. The HTTP Slug header value if present
  3. A default filename chosen by Google Documents

Currently you can upload word processor documents, spreadsheets and presentations. A list of accepted formats can be found here.

Uploading a spreadsheet

Here is an example of uploading a spreadsheet which will be called Example Spreadsheet on Google Documents.

string doctitle = 'upload sample.csv';
string csvBody = 'name,city,state' + '\n' + 'bill,new york,new york';
GoogleData sheet = service.uploadDocument(
            csvBody,
            doctitle,
            'text/csv'); 
sheet.dump();         

If the upload is successful, a 201 Created response will be returned along with a Atom entry describing the document on the server.

The resulting output shows that the sheet was created

feed-> {openSearch=http://a9.com/-/spec/opensearchrss/1.0/}
   id->http://docs.google.com/feeds/acl/private/full/spreadsheet%3Apj6WC-U7TBq8NXf_jkYIZTg {}
   updated->2008-06-18T21:46:10.935Z {}
   category-> {scheme=http://schemas.google.com/g/2005#kind, term=http://schemas.google.com/acl/2007#accessRule}
   link-> {href=http://docs.google.com/feeds/acl/private/full/spreadsheet%3Apj6WC-U7TBq8NXf_jkYIZTg, rel=http://schemas.google.com/g/2005#feed, type=application/atom+xml}
   link-> {href=http://docs.google.com/feeds/acl/private/full/spreadsheet%3Apj6WC-U7TBq8NXf_jkYIZTg, rel=http://schemas.google.com/g/2005#post, type=application/atom+xml}
   openSearch:totalResults->1 {}
   entry-> {}
      id->http://docs.google.com/feeds/acl/private/full/spreadsheet%3Apj6WC-U7TBq8NXf_jkYIZTg/user%3Asforcedemos%40example.com {}
      category-> {scheme=http://schemas.google.com/g/2005#kind, term=http://schemas.google.com/acl/2007#accessRule}
      link-> {href=http://docs.google.com/feeds/acl/private/full/spreadsheet%3Apj6WC-U7TBq8NXf_jkYIZTg/user%3Asforcedemos%40example.com, rel=self, type=application/atom+xml}
      link-> {href=http://docs.google.com/feeds/acl/private/full/spreadsheet%3Apj6WC-U7TBq8NXf_jkYIZTg/user%3Asforcedemos%40example.com, rel=edit, type=application/atom+xml}
      gAcl:role-> {gAcl=http://schemas.google.com/acl/2007, value=owner}
      gAcl:scope-> {gAcl=http://schemas.google.com/acl/2007, type=user, value=sforcedemos@example.com}    

Searching the documents feed

You can search the Document List using some of the standard Google Data API query parameters. Categories are used to restrict the type of document (word processor document, spreadsheet) returned. More detailed information on parameters specific to the Documents List can be found in the Documents List Data API Reference Guide. Please note that the DocList API currently does not support excluding folders using the /category/-starred syntax.

Retrieving all word processor documents

A list of only word processor documents can be retrieved from Apex code by using the document category as follows:

 GoogleData documents = service.getFeed ( DocumentService.feedUrl + '-/document' ); 
 documents.dump(); 

For an example entry of the feed that will be returned, look at the Retrieving a list of documents section (above).

Retrieving all starred presentations

A list of only starred presentations can be retrieved by using the presentation and starred categories as follows:

GoogleData documents = service.getFeed ( 
                             DocumentService.feedUrl + '-/presentation/starred' ); 
documents.dump(); 

Retrieving all documents in a named folder

A list of only documents contained in a folder named "starred" created by the current user with email address email can be retrieved by using a scheme qualified folder category to disambiguate from a starred category query for starred documents as follows:

GoogleData documents = service.getFeed ( 
	DocumentService.feedUrl + 
	'-/{http:%2F%2Fschemas.google.com%2Fdocs%2F2007%2Ffolders%2Femail}starred'); 
documents.dump(); 

Performing a text query

You can search the content of documents by using the q query parameter on the feed. For example, if you wanted to search a user's documents for the string "example query" you could perform the following Apex code:

GoogleData documents = service.getFeed ( 
DocumentService.feedUrl + '?q=example+query'); 
documents.dump(); 

A more detailed description of how the q parameter works can be found on the Google Data APIs Reference Guide.

Related Content