Importing Google Contacts into Asp.Net

Raj.Trivedi
Posted by in ASP.NET category on for Intermediate level | Points: 250 | Views : 4879 red flag

Hello Team,

In this Article we will try to import our Gmail Contacts in a Grid View

Introduction


Hello Team,

We all know that Gmail is the most used Email Service.There might be a situation where you will need to create a small app where we might have to Import Contacts from Gmail User Name.



Objective


To obtain Gmail Contacts from Gmail User Name.



Using the code


Pre requisites:-

  1. Google has given ready made libraries in order to Import Contacts from GMail.
  2. We can Download the Google Libraries from 
http://google-gdata.googlecode.com/files/Google%20Data%20API%20Setup%281.4.0.2%29.msi

Once you Extract the Set up will get these 4 dlls as shown in the screen below




Let us start the Application Creation Now.

  1. Create a New Project in Visual Studio 2012 -> Empty Web Application
  2. Right Click on References and Add all these 3 Dlls to the references.
  3. Now take 2 Text Boxes and 1 Button and 1 Gridview
  4. The first text box will take Your Gmail User Name and Second will take input for your Password.
  5. A button to send Request to Google using the Google API for fetching the contacts and feeding it to Gridview.
  6. A gridview to Bind the contacts in the Application

THE UI Screen



Here is the detail Explanation of the Code

  1. We will create a Function named GetGmailContacts having 3 Parameter such as  string ApplicationName,string GmailID,string GmailPassword.
  2. Creation of dataset to store the Gmail Contacts.
  3. Creation of Data table to pass it to Dataset that we have created above
  4. Creating Data Column in the data table.This column will also act as a Column to Gridview
  5. Setting The Type of the Column to string as we are getting email as data.
  6. Adding the data column to the table.
  7. Creating an Object of Request Setting Class which is a Class in Google.GData.Client that will accept 3 Parameters i.e. ApplicationName, GmailID, GmailPassword.
  8. Creating an Object of ContactsRequest which is a class of Google.Contacts that will take the RequestSettings object as Parameter and request Google to get the Contact Info
  9. Creating an Object of Feed<Contact> which is a class of Google.GData.Client where we get contacts from Google and feed it to the object of Feed class of type contact.
  10. Now we will run a For Loop on the feedcontact object to scan the entries that are inserted into the object of Contact Class
  11. Now we dynamically generate rows as per the data that we have recieved from Gmail Contacts.
  12. Then add the Rows to the data table
  13. Finally we add the data table to dataset.
  14. Then we will write the Function BindContactstoGrid() to bind the data to grid view that we have stored in the data set
  15. In this function we will create a new Dataset object and to that object will assign GetGmailContacts which is data set type.
// HTML Mark UP and Code Behind
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ImportGoogleContacts.aspx.cs" Inherits="Importing_Google_Contacts.ImportGoogleContacts" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .style1
        {
            width: 100%;
        }
        .style2
        {
            width: 116px;
        }
        .style3
        {
            width: 258px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div align="center">
    
        <table class="style1">
            <tr>
                <td class="style2">
                    Email :-</td>
                <td class="style3">
                    <asp:TextBox ID="xtxtGmail" runat="server" Width="234px"></asp:TextBox>
                </td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td class="style2">
                    Password:-</td>
                <td class="style3">
                    <asp:TextBox ID="xtxtPassword" TextMode="Password" runat="server" Width="226px"></asp:TextBox>
                </td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td class="style2">
                    &nbsp;</td>
                <td class="style3">
                    <asp:Button ID="btnImport" runat="server" Text="Import Google Contacts " 
                        onclick="btnImport_Click" />
                </td>
                <td>
                    &nbsp;</td>
            </tr>
        </table>
    
    </div>
    <br />
    <br />
    <div align="center">
    <asp:GridView id="gvGmail" runat ="server"></asp:GridView>
    </div>
    </form>
</body>
</html>																								
// Code Behind
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Google.GData.Client; 
using Google.Contacts;
using Google.GData.Extensions;

namespace Importing_Google_Contacts
{
    public partial class ImportGoogleContacts : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnImport_Click(object sender, EventArgs e)
        {
            BindContactstoGrid();
        }

        private DataSet GetGmailContacts(string ApplicationName,string GmailID,string GmailPassword)
        {
            // Creation of dataset to store the Gmail Contacts.
            DataSet dsGmail = new DataSet();

            // Creation of Data table to pass it to Dataset that we have created above
            DataTable dtemail = new DataTable();

            // Creating Data Column in the data table.This column will also act as a Column to Gridview
            DataColumn dcemail = new DataColumn();

            // The Type of the Column is string as we are getting email as data.
            dcemail.DataType = Type.GetType("System.String"); 
            dcemail.ColumnName = "EmailID";

            // Over here we are adding the data column to the table.
            dtemail.Columns.Add(dcemail); 

           // Creating an Object of Request Setting Class which is a Class in Google.GData.Client that will accept 3 Parameters i.e. ApplicationName, GmailID, GmailPassword.
            RequestSettings rs = new RequestSettings(ApplicationName, GmailID, GmailPassword);
            rs.AutoPaging = true;

            // Creating an Object of ContactsRequest which is a class of Google.Contacts that will take the RequestSettings object as Parameter and request Google to get the Contact Info
            ContactsRequest cr = new ContactsRequest(rs);

            // Creating an Object of Feed<Contact> which is a class of Google.GData.Client where we get contacts from Google and feed it to the object of Feed class of type contact.
            Feed<Contact> feedcontacts = cr.GetContacts();

            // Now we will run a For Loop on the feedcontact object to scan the entries that are inserted into the object of Contact Class
            foreach (Contact con in feedcontacts.Entries)
            {
                foreach (EMail GmailContacts in con.Emails)
                {
                    // Over here we dynamically generate rows as per the data that we have recieved from Gmail Contacts.
                    DataRow dr1 = dtemail.NewRow();
                    dr1["emailid"] = GmailContacts.Address.ToString();

                    // We here add the Rows to the data table
                    dtemail.Rows.Add(dr1); 

                }
            }
            // Finally we add the data table to dataset.
            dsGmail.Tables.Add(dtemail); 
            return dsGmail;
            
        }


        // Function to bind the data to grid view that we have stored in the data set
        private void BindContactstoGrid()
        {
            DataSet dsgrid = new DataSet();
            // Passing the GetGmailContacts to the data set
            dsgrid = GetGmailContacts("Import Gmail Contacts", xtxtGmail.Text.Trim(), xtxtPassword.Text.Trim());
            gvGmail.DataSource = dsgrid;
            gvGmail.DataBind();

        }


    }
}


Output Screen





Note :-The fetching of Contacts might take time depending upon your Contacts list.So you might have to be a little patient.One more thing you also need the Internet Connectivity up in running for successful Execution.

Hope this will be a valuable information


Page copy protected against web site content infringement by Copyscape

About the Author

Raj.Trivedi
Full Name: Raj Trivedi
Member Level:
Member Status: Member,MVP
Member Since: 6/16/2012 2:04:41 AM
Country: India
Regard's Raj.Trivedi "Sharing is Caring" Please mark as answer if your Query is resolved
http://www.dotnetfunda.com/profile/raj.trivedi.aspx
Raj Trivedi i.e. me started my career as Support Professional and then moved on the Software development eventually reached at these skills Software Development | Enthusiastic Blogger | Content Writer | Technical Writer | Problem Solver | Lecturer on Technology Subjects | Runnerup Award Winner on www.dotnetfunda.com and firm believer in Sharing as a way of Caring Yet this much achieved its still a long way to go and there is biggest dream lying to be one of the best entrepreneurs of India in Technology Department. The Dream has just started and i hope it follows. Highlights are mentioned in details in my profile at http://in.linkedin.com/pub/raj-trivedi/30/61/b30/

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)