Monday, 27 June 2011

Retrieving Contacts from Gmail using Google Contacts Data API in .NET

Following is the sample code to Retrieve the Contact information from your Gmail account in .NET applicaton
For more details visit:  http://code.google.com/apis/contacts/docs/3.0/developers_guide_dotnet.html

Retrieve the Name and email and Display using Repeater Control:

Code behind:

protected void GetGmailContact()
{
        string strName = string.Empty;
        string strEmail = string.Empty;

        RequestSettings rs = new RequestSettings("Test Application", txtEmail.Text, txtPwd.Text);
        ContactsRequest cr = new ContactsRequest(rs);
        rs.AutoPaging = true;

        Feed<Contact> fd = cr.GetContacts();

        DataSet ds = new DataSet();

        DataTable dt = ds.Tables.Add();
        dt.Columns.Add("Name", typeof(string));
        dt.Columns.Add("Email", typeof(string));

        foreach (Contact entry in fd.Entries)
        {
            Name name = entry.Name;

            strName = name.GivenName;

            foreach (EMail email in entry.Emails)
            {
                strEmail = email.Address;
                // Get one email only
                break;
            }
            dt.Rows.Add(strName, strEmail);

            dt.Rows.Add(strName, strEmail);
        }

        rptrContacts.DataSource = ds;
        rptrContacts.DataBind();
}



ASPX Page:

<div>
    <asp:Label ID="Label1" runat="server" Text="Email Id:"></asp:Label>
    <asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
         <br />
         <asp:Label ID="Label2" runat="server" Text="Password: "></asp:Label>
         <asp:TextBox ID="txtPwd" runat="server" TextMode="Password"></asp:TextBox>
         <br />
         <asp:Button ID="btnContact" runat="server" onclick="btnContact_Click"
             Text="Get Contact" />
         <br />
    <asp:Repeater ID="rptrContacts" runat="server">
    <HeaderTemplate>
    <table>
    </HeaderTemplate>
    <ItemTemplate>
    <tr><td><%#DataBinder.Eval(Container.DataItem, "Name") %> </td>
    <td><%#DataBinder.Eval(Container.DataItem, "Email") %> </td></tr>
    </ItemTemplate>
    <FooterTemplate>
        </table>
    </FooterTemplate>
    </asp:Repeater>
</div>

1 comment:

  1. T.I.D. Furniture
    T.I.D. race tech titanium Furniture is a company that specializes in making wood titanium max and aluminum used to make titanium plate flat irons jewelry, furnishing, furniture, micro touch titanium trim furniture and titanium daith jewelry more.

    ReplyDelete