Monday, October 8, 2012

FQL - Getting all your friends


In my last post I showed you how to get and display your facebook details, in this post I'll show you how to display the details of all your friends.

Same as last time we need to understand what are the relevant tables and which columns interest us. We'll go to http://developers.facebook.com/docs/reference/fql/friend/and take a look at the FRIEND table:


Its a really simple table with just two columns: the logged in user id and the friend's id.
SELECT uid1, uid2 FROM friend WHERE uid1 = me() AND uid2 = 220439

Using this query you can easily determine if two users are friends
The me() method returns the id of the logged in user and the second id is the other user id, if the query returns an answer then the two users are connected – friends.

Let's return to other main topic: Displaying all my friend's details. Every friendis eventually a user; the FRIEND table holds only the users ids but not their details. In order to get their details we'll have first to get all the friends ids and then query the USER table with those ids.

This query gets all my friends:

SELECT uid2 FROM friend WHERE uid1 = me()


We'll create a sub-select to retrieve all the details off all my friends:

SELECT first_name, last_name, pic_square FROM user WHERE uid in 
(SELECT uid2 FROM friend WHERE uid1 = me())

Now after we know how to get the friends details let's code!


Since eventually we'll retrieve a list of users we'll use the same model from last time – FacebookUserModel, this name will suite more: FacebookUsersModel.



Same as before the model holds a list of FacebookUser that represents the wanted fields from the USER table.

Now let's create the controller:



Connect to facebook servers and run the query then get the data from the dynamic object,  desirialize the Json and send the model to the view.

The view needs to be strongly-typed viewso we'll have access to the model's properties that we want to display: pic_square, first_name and last_name:



(I love MVC- using .net features and html tags together)

F5 the SLN and navigate to the Friends controller








There you have it!

All your friends on one page. If you want to split them for pages stay tuned for my next post–"Paging and FQL"





No comments:

Post a Comment