Full meaning of FQL is ‘Facebook Query Language’. This is used to retrieve filtered data from facebook api similarly as sql query works with database. You won’t find it exactly like SQL query syntax all the time, and not all kind of operations too. But, it definitely meets the demand. In this tutorial, i will try to explain using fql in your .NET application with c# code examples where applicable. I am assuming, you are familiar with using facebook graph api through c#. If you are a beginner Facebook api developer in c#, you should better start by another article about getting started with Facebook graph api in c#. Also, you should be familiar with facebooksdk library also to use the examples here. If not yes, please read another tutorial about getting started with facebooksdk/c#
Why FQL?
Simple answer is: for performance tuning, make api calls faster and get data only which are needed(filters in the ways where general graph api can’t). Suppose, if you need some filtering while retrieve friends which meets a certain criteria and also, need only certain information, then fql will perform better for you then general api call. Because, with graph api call, you will get a lot of information and you will have to filter them on your end. so, time between making the api call and get the response will increase in a huge amount, specially because of the amount of data to be transferred.
SQL Operations Supported By FQL:
You can only make select operations. Naturally, you won’t be able to make any insert/update/delete operations. Moreover, even you won’t be able to do any join operations too. You will have to find alternative ways for this. Like, you can execute multiple query on a single request. Also, its require that, you must using ‘WHERE’ condition for filtering results. otherwise it won’t work. Like, if you just want to retrieve all user’s all info, you aren’t gonna get the result, right?
Understanding index-able columns and their purposes:
If you look at the facebook documentation’s FQL section and enter to any table, you should see that, one or more column(s) on every tables are marked as ‘*’ . Those columns are known as ‘Index-able’ columns. To use FQL, one of your prerequisite is that you will have to use any one(at least) or more index-able column(s) in ‘WHERE’ condition.
Simple FQL Operation Syntax in C# FacebookSDK:
Here is a very simple fql operation example with using c#/faceboksdk code, which will retrieve all the female friends of yours:(code snippet is from a mvc web application, but you will get clear idea how to use the fql)
//will return a facebook session object public FacebookSession FacebookSession { get { return (new CanvasAuthorizer().Session); } } public MyController() { try { //create a new instance of facebook app var fbApp = new FacebookClient(this.FacebookSession.AccessToken); //basic graph api call to retrive user's information dynamic result = fbApp.Get("me"); ViewData["Firstname"] = result.first_name; ViewData["Lastname"] = result.last_name; //basic fql query execution dynamic friends = fbApp.Query("SELECT uid, name, pic_square FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me()) AND sex='female'"); //loop through all friends and get their name and create response containing friends' name and profile picture foreach( dynamic friend in friends ) { result += friend.name+"<img src='"+friend.pic_square+"' alt='"+friend.name+"' /><br />"; } } catch { //error handling code } }
Other Operators:
You can also use ‘Limit’ keyword in your FQL exactly in the same way as you did on SQL. However, there is no support for ‘Like’ keyword. If you want to use those kind of searching operations, then you will have to use ‘strpos’ method inside the fql like as follows:
SELECT uid, name, pic_square FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me()) AND strpos(name,'abc')>=0
The above fql query will return you all the friends who have ‘abc’ keyword in their name.
Execute FQL Asynchronously:
You can also execute fql query asynchronously in c#. To achieve this, we just will have to use ‘QueryAsync’ method instead of ‘Query’ and pass extra parameter for the callback handler method.
References:
Hope this small fql tutorial is helpful on the way of your facebook application development. Please visit official facebook documentation on fql for details. Don’t hesitate to ask me if you have any questions. Happy coding 🙂
andreauxyo says
where does the FacebookSession and CanvasAuthorizer come from?
Rana says
Hi, that is part of facebooksdk library. I noticed that, i didn’t mentioned about that before, just found from your comment. Thanks for that and i added about it at the first paragraph. Hope this helps.
Sriya says
The SDK does not contain a Query method that U’ve used. Where do I get the source for that?
Rana says
This ‘Facebooksdk’ is not the core official library, its an open source project on codeplex here: http://facebooksdk.codeplex.com/ . I have already discussed how to start using facebooksdk here: https://wordpress-1325650-4848760.cloudwaysapps.com/development/c-sharp-library-for-facebook-api . Hope this helps.
Matt says
I can’t find any examples of calling QueryAsync and passing a callback method as a parameter. The signature seems to be FacebookClient.QueryAsync(string fql, object userToken). Is the usertoken supposed to be the callback? Every example I’ve seen defines a single GetCompleted Event to handle the callback for QueryAsync, but doesn’t pass anything into the QueryAsync. The problem is if I have multiple QueryAsync calls they all return to the same Getcompleted event, and I would like a different completed event for every QueryAsync(strFQL) call, because the logic in the completed event would be different based on what you return from FQL. Could you please point me in the right direction on how to do this, as it’s been very difficult to find examples.
Thanks
robert says
i installed facebook-c#-sdk via nuget package installer and i don’t have fb.Query method i only have fb.Get how do i do this?
India says
Awesome mate! Worked like a charm.
Narendra Jarad says
Awesome dear.thanks for sharing your idea.
Kinjal says
Is it possible to search any body’s information through their Email id using FQL? As i came to know that this facility is now stopped by face book itself.
architster says
Hi,
Since FQL is going to obsolete in newer version of facebook platform API after version 2.0.
1) Can you please guide which version of facebook platform API is getting currently used in this facebook.dll ?
2) How can we migrate to newer versions of Facebook platform API if it is not 2.0.
If we were getting facebook image feed using FQL how can we get the same using Graph API call with parameters like page id for which we need the image feed, no of feeds required count,
Thanks in advance