Tuesday, May 19, 2009

Facebook - Linq to Fql

Step 1: Requirements

1. Download the Facebook LINQ to FQL binaries(DLLs) from http://facebooklinq.codeplex.com/
2. Create a website or application in VS 2008
3. Add references of DLL (downloaded in step1) in to the project.

Step 2: Facebook Application Setting

4. Login in http://www.Facebook.com with valid user name and password.
5. Paste the URL http://www.facebook.com/developers/ to support custom application in your account.
6. Click “Allow” button to allow for custom Facebook application
7. Click “Setup New Application” button to validate your custom application
8. Give the application name – let’s say “My Application” and select “Agree” radio button then click “Save Changes” button.
8. Go to "Canvas" tab and enter the "Canvas Callback URL". Let's say "http://localhost:1111/FBDemo/Default.aspx
9. On the same tab, select Render Method as IFrame from "Canvas Setting" section.
10. Facebook generates some credentials for the created application like “API Key”, “Application Secret”,” Application ID” with respective values. See below fig


Step 3: Setting the Facebook application credentials in Web.Config

11. Add below values in “appSettings” section

12.Add below value in “httpHandlers” section

Step 4: Accessing Facebook Data

13. Open the webpage (in our case Default.aspx)
14. Add reference of Facebook dlls using below lines
using facebook;
15. Authenticate current Facebook Logged in user with below line
bool isAuthenticated = facebook.Web.FacebookContext.Current.TryAuthenticating(true);
16. After authentication, create the object of FacebookDataContext with below line
var db = new FacebookDataContext();
17. Now access the properties of db whose type of facebook.Linq.FqlTable indicates the facebook
data like;
a. db.user – user information
b. db.group – groups information
c. db.friend_info – friends list etc.
18. Filter data as per requirements through executing LINQ on the above lists

Step 5: Source code listing
a. ASPX Page
Put the DataList and add item templates in to the datalist as per requirements

b. Source Listing (C# Code):
Write the below code to display the logged in user information like name, birthday and photo of the user.
protected void Page_Load(object sender, EventArgs e)
{
try
{
if (facebook.Web.FacebookContext.Current.TryAuthenticating(true))
{
var db = new FacebookDataContext();
var myUser = from user in db.user where user.uid == db.uid select user;
var myUser2 = db.user.Where(t => t.uid == db.uid);
Example1DataList.DataSource = myUser.Take(10);
DataBind();
}
}
catch (Exception ex)
{
throw ex;
}
}

No comments:

Post a Comment