ARTICLE

Fetch Gmail Contacts

Posted by Surya Prakash Articles | ASP.NET Programming November 23, 2011
In this article, we will see how to fetch a Gmail contacts list & show it in a grid view.
Reader Level:

You Need to follow below steps to fetch a Gmail contacts list & show it in a grid view.

  1. Download Google Data API Dlls
  2. Extract Google MSI component
  3. Implement Code to fetch contacts

Download Google Data API Dlls: Open the below link & click, download Google_Data_API_Setup_1.9.0.0.msi
http://code.google.com/p/google-gdata/downloads/list

fetch Gmail contacts

Extract Google MSI

Double click the downloaded MSI component & provide the path to extract the code. Usually it will extract into c:\Program file.

To fetch the DLL, go to C:\Program Files\Google\Google Data API SDK\Samples

Implement Code to fetch Contacts


Create a new website

Copy the below dlls from the above sample path & add below dll to your solution

  1. Google.GData.Client
  2. Google.GData.Contacts
  3. Google.GData.Extensions

Add a new webpage to solution & name it "contactlist.aspx". Now add the below grid view to the page

<asp:GridView ID="GridView1" AutoGenerateColumns="true"
runat="server" BackColor="Aquamarine" ForeColor="Black" BorderColor="OrangeRed"
Font-Names="Comic Sans MS" Width="525">
       <AlternatingRowStyle BackColor="Aqua" />
       <HeaderStyle BackColor="Crimson"
        Font-Italic="false" ForeColor="Snow" />
</asp:GridView> 


Go to .aspx.cs page and following code & also add following to the class

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

private void FetchContactList()
{      
      
// Define string of list
      List<string> lstContacts = new List<string>();
 
      
// Below requestsetting class take 3 parameters applicationname, gmail username, gmail password. Provide appropriate Gmail account details
      RequestSettings rsLoginInfo = new RequestSettings("", "suryabg2000@gmail.com", "XXXXXX");
      rsLoginInfo.AutoPaging = true
      ContactsRequest cRequest = new ContactsRequest(rsLoginInfo);
 
      
// fetch contacts list
      Feed<Contact> feedContacts = cRequest.GetContacts();
 
      
// looping the feedcontact entries
      foreach (Contact gmailAddresses in feedContacts.Entries)
      { 
         
// Looping to read email addresses
          foreach (EMail emailId in gmailAddresses.Emails)
          {             
              lstContacts.Add(emailId.Address);
          }
      }
      
// finally binding the list to gridview defined in above step

      GridView1.DataSource = lstContacts;
      GridView1.DataBind(); 
}


Finally you will be able to see the data in the gridview.

Happy coding... Hope this helps!

COMMENT USING