Author : Prakash Pradeep Gopu
In this post I am going to explain about how to import the Gmail contact list in to our application and Binding in to the Gridview.I have observed Many Social networking sites or many sites when we give the User name and password of gmail account they are able to fetch the our contact list.So I want to explain this in dotnet and binding it to in grid view.
After add namespaces write the following code in Code behind file :
In this post I am going to explain about how to import the Gmail contact list in to our application and Binding in to the Gridview.I have observed Many Social networking sites or many sites when we give the User name and password of gmail account they are able to fetch the our contact list.So I want to explain this in dotnet and binding it to in grid view.
To do this first you need to download the following 3 DLL’s from the http://code.google.com/p/google-gdata/downloads/list --> Google_Data_API_Setup_1.9.0.0.msi and install in your pc. Then you go to the path where you installed the google data and you will find the following Dll’s.
Google.GData.Contacts.dll,Google.GData.Client.dll,Google.GData.Extensions.dll
Add the above Dll’s to your application.
Now You need to design your Aspx page with the following code :
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.style1
{
width: 100%;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table class="style1">
<tr>
<td width="10%">
</td>
<td width="40%">
</td>
<td width="40%">
</td>
<td width="10%">
</td>
</tr>
<tr>
<td>
</td>
<td>
Gmail Email Id</td>
<td>
<asp:TextBox ID="txtgmailid" runat="server" Width="297px"></asp:TextBox>
</td>
<td>
</td>
</tr>
<tr>
<td>
</td>
<td>
Gmail Pwd</td>
<td>
<asp:TextBox ID="txtPwd" runat="server" TextMode="Password" Width="297px"></asp:TextBox>
</td>
<td>
</td>
</tr>
<tr>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td>
</td>
<td align="center" colspan="2">
<asp:Button ID="Button1" runat="server" onclick="Button1_Click"
Text="Get Contacts" />
</td>
<td>
</td>
</tr>
<tr>
<td>
</td>
<td colspan="2">
<asp:GridView ID="GridView1" runat="server"
BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px"
CellPadding="3" Width="100%">
<FooterStyle BackColor="White" ForeColor="#000066" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
<RowStyle ForeColor="#000066" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#007DBB" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#00547E" />
</asp:GridView>
</td>
<td>
</td>
</tr>
<tr>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
After Adding the above code in the Aspx page and include the following namespaces in your application :
using Google.GData.Client;
using Google.GData.Extensions;
using Google.GData.Contacts;
using Google.Contacts;
using System.Collections;
using System.Data;
using System.Xml;
using System.Text;
using System.Net;
using System.Net.Mail;
After add namespaces write the following code in Code behind file :
string mailid = "";
string pwd = "";
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
mailid = txtgmailid.Text;
pwd = txtPwd.Text;
BindGridview(mailid, pwd);
}
private void BindGridview(string Userid,string Password)
{
//Application Name,Username,password
RequestSettings rs = new RequestSettings("ApplicationName", mailid, pwd);
//Request all contacts
ContactsRequest cr = new ContactsRequest(rs);
rs.AutoPaging = true;//Allow autopaging
Feed<Contact> f = cr.GetContacts();//Get all contacts
DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add("Name");
dt.Columns.Add("Home Emails");
dt.Columns.Add("Work Emails");
dt.Columns.Add("Other Emails");
dt.Columns.Add("Home Phone");
dt.Columns.Add("Work Phone");
dt.Columns.Add("Other");
dt.Columns.Add("URL");
foreach (Contact ex in f.Entries)
{
dr = dt.NewRow();
Name n = ex.Name;
dr[0] = n.FullName;
string homeemail = "";
string workemail = "";
string otheremail = "";
string homephone = "";
string workphone = "";
string otherphone = "";
foreach (EMail email in ex.Emails)
{
if (email.Home == true)
{
if (homeemail.Equals(""))
{
homeemail += email.Address;
}
else
{
homeemail += ",";
homeemail += email.Address;
}
}
if (email.Work == true)
{
if (workemail.Equals(""))
{
workemail += email.Address;
}
else
{
workemail += ",";
workemail += email.Address;
}
}
else
{
if (otheremail.Equals(""))
{
otheremail += email.Address;
}
else
{
otheremail += ",";
otheremail += email.Address;
}
}
dr[3] = otheremail;
}
//Extract Phone Numbers
foreach (PhoneNumber ph in ex.Phonenumbers)
{
if (ph.Home == true)
{
if (homephone.Equals(""))
{
homephone += ph.Value;
}
else
{
homephone += ",";
homephone += ph.Value;
}
}
else if (ph.Work == true)
{
if (workphone.Equals(""))
{
workphone += ph.Value;
}
else
{
workphone += ",";
workphone += ph.Value;
}
}
else
{
if (otherphone.Equals(""))
{
otherphone += ph.Value;
}
else
{
otherphone += ",";
otherphone += ph.Value;
}
}
dr[4] = homephone;
dr[5] = workphone;
dr[6] = otherphone;
}
dt.Rows.Add(dr);
}
GridView1.DataSource = dt;
GridView1.DataBind();
}
No comments:
Post a Comment