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:-
- Google has given ready made libraries in order to Import Contacts from GMail.
- 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.
- Create a New Project in Visual Studio 2012 -> Empty Web Application
- Right Click on References and Add all these 3 Dlls to the references.
- Now take 2 Text Boxes and 1 Button and 1 Gridview
- The first text box will take Your Gmail User Name and Second will take input for your Password.
- A button to send Request to Google using the Google API for fetching the contacts and feeding it to Gridview.
- A gridview to Bind the contacts in the Application
THE UI Screen
Here is the detail Explanation of the Code
- We will create a Function named GetGmailContacts having 3 Parameter such as string ApplicationName,string GmailID,string GmailPassword.
- Creation of dataset to store the Gmail Contacts.
- Creation of Data table to pass it to Dataset that we have created above
- Creating Data Column in the data table.This column will also act as a Column to Gridview
- Setting The Type of the Column to string as we are getting email as data.
- Adding the data column to the table.
- 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.
- 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
- 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.
- Now we will run a For Loop on the feedcontact object to scan the entries that are inserted into the object of Contact Class
- Now we dynamically generate rows as per the data that we have recieved from Gmail Contacts.
- Then add the Rows to the data table
- Finally we add the data table to dataset.
- Then we will write the Function BindContactstoGrid() to bind the data to grid view that we have stored in the data set
- 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>
</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>
</td>
</tr>
<tr>
<td class="style2">
</td>
<td class="style3">
<asp:Button ID="btnImport" runat="server" Text="Import Google Contacts "
onclick="btnImport_Click" />
</td>
<td>
</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