In this article I will explain how to fetch and import FaceBook friends and contacts in ASP.Net using the Free ASPSnippets FaceBook API.
Note:
You can download the latest ASPSnippets.FaceBookAPI.dll clicking the download link below.
Create FaceBook Application and get App Id
You will need to create an application and get an API Key and API Secret
HTML Markup
In the below HTML Markup I have placed an ASP.Net Button which will trigger the fetching process and an ASP.Net GridView control that will display the fetched friends and contacts along with picture.
<asp:Button ID="btnFetch" runat="server" Text="Fetch Friends" OnClick="btnFetch_Click" />
<br />
<asp:GridView ID="gvFriends" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:ImageField DataImageUrlField="PictureUrl" HeaderText="Picture" />
<asp:BoundField DataField="Name" HeaderText="Name" />
</Columns>
</asp:GridView>
Namespaces
You will need to import the following Namespaces
C#
using ASPSnippets.FaceBookAPI;
using System.Collections.Generic;
using System.Web.Script.Serialization;
VB.Net
Imports ASPSnippets.FaceBookAPI
Imports System.Collections.Generic
Imports System.Web.Script.Serialization
Data Classes
You will need to create the following two classes which will be used to hold the friends and contacts returned from FaceBook.
C#
public class FaceBookData
{
public
TaggableFriends Taggable_Friends{ get; set; }
}
public class TaggableFriends
{
public List<FaceBookUser> Data { get; set; }
}
public class FaceBookUser
{
public string Id { get; set; }
public string Name { get; set; }
public Picture Picture { get; set; }
public string PictureUrl
{
get
{
return this.Picture.Data.Url;
}
}
}
public class Picture
{
public FaceBookUserPicture Data { get; set; }
}
public class FaceBookUserPicture
{
public string Url { get; set; }
}
VB.Net
Public Class FaceBookData
Public Property
Taggable_Friends() As
TaggableFriends
End Class
Public Class TaggableFriends
Public Property Data() As List(Of FaceBookUser)
End Class
Public Class FaceBookUser
Public Property Id() As String
Public Property Name() As String
Public Property Picture As Picture
Public ReadOnly Property PictureUrl() As String
Get
Return Me.Picture.Data.Url
End Get
End Property
End Class
Public Class Picture
Public Property Data() As FaceBookUserPicture
End Class
Public Class FaceBookUserPicture
Public Property
Url() As String
End Class
Fetching FaceBook Friends and Contacts with Profile Pictures and displaying in GridView
On the Fetch button click we first need to authorize our application using FaceBook Graph API and get permission of the user. Once the application is authorized in the page load event based on the authorization code we fetch the FaceBook contacts and friends for the user and then display them in ASP.Net GridView along with their profile pictures.
C#
protected void Page_Load(object sender, EventArgs e)
{
FaceBookConnect.API_Key = "<Your API Key>";
FaceBookConnect.API_Secret = "<Your API Secret>";
if (!IsPostBack)
{
string code = Request.QueryString["code"];
if (!string.IsNullOrEmpty(code))
{
string data = FaceBookConnect.Fetch(code,
"me?fields=taggable_friends");
FaceBookData facebookData = new JavaScriptSerializer().Deserialize<FaceBookData>(data);
gvFriends.DataSource = facebookData.Taggable_Friends.Data;
gvFriends.DataBind();
}
}
}
protected void btnFetch_Click(object sender, EventArgs e)
{
FaceBookConnect.Authorize("user_photos,user_friends", Request.Url.AbsoluteUri);
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
FaceBookConnect.API_Key = "<Your API Key>"
FaceBookConnect.API_Secret = "<Your API Secret>"
If Not IsPostBack Then
Dim code As String = Request.QueryString("code")
If Not String.IsNullOrEmpty(code) Then
Dim data As String = FaceBookConnect.Fetch(code, "me?fields=taggable_friends")
gvFriends.DataSource = facebookData.Taggable_Friends.Data
gvFriends.DataBind()
End If
End If
End Sub
Protected Sub btnFetch_Click(sender As Object, e As EventArgs)
FaceBookConnect.Authorize("user_photos,user_friends", Request.Url.AbsoluteUri)
End Sub
Downloads
You can download the complete source code in VB.Net and C# along with the ASPSnippets.FaceBookAPI DLL using the download link provided below.
Download Code