Google Contact Detail Listing (C# , Google API, ASP.NET)


Check out the following ASP.NET example for listing contact of a gmail account and adding a new contact to it. First download the Google Data API from the specified link and install it

http://code.google.com/p/google-gdata/

Then create a new project and include following references in it

Google.GData.Client.dll
Google.GData.Contacts.dll
Google.GData.Extensions.dll

Create the following class


using System;
using System.Collections.Generic;
using Google.GData.Contacts;
using Google.GData.Extensions;

/// 
/// Summary description for GoogleContactService
/// 
public class GoogleContactService
{
public static ContactsService GContactService = null;

public static void InitializeService(string username, 
string password)
{
GContactService = new ContactsService("Contact Infomation");
GContactService.setUserCredentials(username, password);
}

public static List<ContactDetail> GetAllContact()
{
List<ContactDetail> contactDetails = new List<ContactDetail>();
ContactsQuery query = new ContactsQuery(ContactsQuery.
CreateContactsUri("default"));

ContactsFeed feed = GContactService.Query(query);
foreach (ContactEntry entry in feed.Entries)
{
ContactDetail contact = new 
ContactDetail{Name = entry.Title.Text,
EmailAddress1 = entry.Emails.Count >= 1 ? entry.Emails[0].Address : "",
EmailAddress2 = entry.Emails.Count >= 2 ? entry.Emails[1].Address : "",
Phone = entry.Phonenumbers.Count >= 1 ? entry.Phonenumbers[0].Value : "",
Address =  entry.PostalAddresses.Count >= 1 ? entry.PostalAddresses[0].Value : "",
Details = entry.Content.Content};

contactDetails.Add(contact);
}

return contactDetails;
}

public static void AddContact(ContactDetail contact)
{
ContactEntry newEntry = new ContactEntry();
newEntry.Title.Text = contact.Name;

EMail primaryEmail = new EMail(contact.EmailAddress1);
primaryEmail.Primary = true;
primaryEmail.Rel = ContactsRelationships.IsWork;
newEntry.Emails.Add(primaryEmail);

EMail secondaryEmail = new EMail(contact.EmailAddress2);
secondaryEmail.Rel = ContactsRelationships.IsHome;
newEntry.Emails.Add(secondaryEmail);

PhoneNumber phoneNumber = new PhoneNumber(contact.Phone);
phoneNumber.Primary = true;
phoneNumber.Rel = ContactsRelationships.IsMobile;
newEntry.Phonenumbers.Add(phoneNumber);

PostalAddress postalAddress = new PostalAddress();
postalAddress.Value = contact.Address;
postalAddress.Primary = true;
postalAddress.Rel = ContactsRelationships.IsHome;
newEntry.PostalAddresses.Add(postalAddress);

newEntry.Content.Content = contact.Details;

Uri feedUri = new Uri(ContactsQuery.CreateContactsUri("default"));

ContactEntry createdEntry = (ContactEntry)GContactService.Insert(feedUri, newEntry);
}
}

public class ContactDetail
{
public string Name { get; set; }
public string EmailAddress1 { get; set; }
public string EmailAddress2 { get; set; }
public string Phone { get; set; }
public string Address { get; set; }
public string Details { get; set; }
}

After that create one webpage to list and create new contacts details in my gmail account. You can download the complete project from this location Download From Here

Login to Gmail Account

Add Contacts to google and list them

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s