Tuesday, September 4, 2007

Use Google Bookmarks in your C# WinForms application

Google has a number of services available which I aim to make API's for that nothing exists for currently. The first of those being Google Bookmarks. After lots of hours spent intercepting HTTP requests I have finally created a control capable of retrieving all bookmarks for a given account. In its current version this control only has the ability to retrieve the bookmarks. It cannot create or modify yet, but this will come with a future version. To date I have not found a single piece of code outside of FireFox extensions for utilizing the Google Bookmarks service. I hope you find this of use.

The least you need to know

There are only 4 steps to implementing this control.

  1. Add the tbsGoogleBookmarks class to your project.
  2. Declare an instance of the class
  3. Define a function to handle the BookmarksRefreshed event
  4. Initialize the class with the username and password

Declare an instance

//Because we will assign an event handler to this you
//will want to declare it globally

tbsGoogleBookmarks gBookmarks = new tbsGoogleBookmarks();

Define an event handler in the Form Load event

private void Form1_Load(object sender, EventArgs e)
{
gBookmarks.BookmarksRefreshed += new RefreshedEventHandler(gBookmarks_BookmarksRefreshed);
}

Handle the event

private void gBookmarks_BookmarksRefreshed(string username)
{
listView1.Items.Clear();

for (int bIndex = 0; bIndex <>
{
ListViewItem iBookmark = new ListViewItem();
iBookmark.Text = gBookmarks.Bookmark(bIndex).Title;
iBookmark.SubItems.Add(gBookmarks.Bookmark(bIndex).URL);
iBookmark.SubItems.Add(gBookmarks.Bookmark(bIndex).Category);
listView1.Items.Add(iBookmark);
}
}

Now we just need to initialize the class and sit back and let it all happen.

gBookmarks.Init("UserName", "Password"); 

When the bookmarks have been retrieved, the BookmarksRefreshed event is fired and your list is updated. This can easily be applied to a Tool Strip or other similar control to build a menu.

To download the project source or sample application, view the article on CodeProject.