Wednesday, June 24, 2009

Google Contacts Sample

From past few days I have been experimenting with Google Data API's to come up with modules to fetch Contacts from Google Servers. After spending couple of hours reading documents from Google, I finaly figured out how to get the module working. So to make things easier for people looking for some sample I am Posting my code here. So kickstart you work with the snippet below.





using System;
using System.Drawing;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;

using Google.GData.Client;
using Google.GData.Extensions;
using Google.GData.Contacts;
using Google.Contacts;

namespace GoogleContacts
{
class Program
{
static void Main(string[] args)
{
GetContacts();
}

static void GetContacts()
{
ContactsService CntService = new ContactsService("GoogleContactsCom-GoogleContactsApp-1");
CntService.setUserCredentials("mail-id@gmail.com", "password");

ContactsQuery CntQuery = new ContactsQuery();
CntQuery.Uri = new Uri("http://www.google.com/m8/feeds/contacts/mail-id@gmail.com/full");

ContactsFeed CntFeed = CntService.Query(CntQuery);

foreach (ContactEntry CE in CntFeed.Entries)
{
Console.WriteLine("Name : " + CE.Title.Text);

for (int i = 0; i < CE.IMs.Count; i++)
{
Console.WriteLine("IMs : " + i + " : " + CE.IMs[i].Value);
}

for (int i = 0; i < CE.Organizations.Count; i++)
{
Console.WriteLine("Orgs : " + i + " : " + CE.Organizations[i].Title.ToString());
}

for (int i = 0; i < CE.Phonenumbers.Count; i++)
{
Console.WriteLine("Ph No : " + i + " : " + CE.Phonenumbers[i].Value);
}

for (int i = 0; i < CE.PostalAddresses.Count; i++)
{
Console.WriteLine("Addr : " + i + " : " + CE.PostalAddresses[i].Value);
}

if (CE.PrimaryEmail != null)
Console.WriteLine("Primary EMail : " + CE.PrimaryEmail.Address);

if (CE.PrimaryIMAddress != null)
Console.WriteLine("Primary IM : " + CE.PrimaryIMAddress.Value);

if (CE.PrimaryPhonenumber != null)
Console.WriteLine("Primary Ph No : " + CE.PrimaryPhonenumber.Value);

if (CE.PrimaryPostalAddress != null)
Console.WriteLine("Primary Postal Addr : " + CE.PrimaryPostalAddress.Value);

Console.WriteLine("\n\n");
}
}
}


}

No comments: