The full meaning of FQL is ‘Facebook Query Language’. This is used to retrieve filtered data from Facebook API, similar to how an SQL query works with a database. You won’t find it exactly like SQL query syntax all the time, and not all kinds 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 assume you are familiar with using Facebook graph API through c#. If you are a beginner Facebook API developer in c#, you should better start with another article about getting started with Facebook graph API in c#. Also, you should be familiar with the FacebookSDK library so you can use the examples here. If not yes, please read another tutorial about getting started with facebooksdk/c#
Why FQL?
The simple answer is: For performance tuning, make API calls faster and get only the data that is needed(filters in the ways where general graph API can’t). Suppose you need some filtering while retrieving friends who meet certain criteria and also need only certain information; then FQL will perform better for you than a general API call. Because, with graph API calls, you will get a lot of information, and you will have to filter them on your end. So, the time between making the API call and getting the response will increase by a huge amount, especially 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, you won’t even be able to do any JOIN operations. You will have to find alternative ways to do this. Like, you can execute multiple queries on a single request. Also, you are required to use the ‘WHERE’ condition to filter 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 any table, you should see that one or more column(s) on every table are marked as ‘*’. Those columns are known as ‘Index-able’ columns. To use FQL, one of your prerequisites is to use any one(at least) or more indexable column(s) in the ‘WHERE’ condition.
Simple FQL Operation Syntax in C# FacebookSDK:
Here is a very simple FQL operation example using c#/FacebokSDK code, which will retrieve all your female friends of:(The code snippet is from a MVC web application, but you will get a clear idea of 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
}
}Code language:JavaScript(javascript)
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 kinds of searching operations, then you will have to use ‘strpos’ method inside the FQL as follows:
SELECT uid, name, pic_square FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me()) AND strpos(name,'abc')>=0Code language:JavaScript(javascript)
The above FQL query will return to you all the friends who have the ‘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 the official Facebook documentation on FQL for details. Don’t hesitate to ask me if you have any questions. Happy coding 🙂
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.
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.
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.
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,
var JetpackInstantSearchOptions=JSON.parse(decodeURIComponent("%7B%22overlayOptions%22%3A%7B%22colorTheme%22%3A%22light%22%2C%22enableInfScroll%22%3Atrue%2C%22enableFilteringOpensOverlay%22%3Atrue%2C%22enablePostDate%22%3Atrue%2C%22enableSort%22%3Atrue%2C%22highlightColor%22%3A%22%23FFC%22%2C%22overlayTrigger%22%3A%22submit%22%2C%22resultFormat%22%3A%22expanded%22%2C%22showPoweredBy%22%3Atrue%2C%22defaultSort%22%3A%22relevance%22%2C%22excludedPostTypes%22%3A%5B%5D%7D%2C%22homeUrl%22%3A%22https%3A%5C%2F%5C%2Fcodesamplez.com%22%2C%22locale%22%3A%22en-US%22%2C%22postsPerPage%22%3A5%2C%22siteId%22%3A18994550%2C%22postTypes%22%3A%7B%22post%22%3A%7B%22singular_name%22%3A%22Post%22%2C%22name%22%3A%22Posts%22%7D%2C%22page%22%3A%7B%22singular_name%22%3A%22Page%22%2C%22name%22%3A%22Pages%22%7D%2C%22attachment%22%3A%7B%22singular_name%22%3A%22Media%22%2C%22name%22%3A%22Media%22%7D%7D%2C%22webpackPublicPath%22%3A%22https%3A%5C%2F%5C%2Fcodesamplez.com%5C%2Fwp-content%5C%2Fplugins%5C%2Fjetpack%5C%2Fjetpack_vendor%5C%2Fautomattic%5C%2Fjetpack-search%5C%2Fbuild%5C%2Finstant-search%5C%2F%22%2C%22isPhotonEnabled%22%3Afalse%2C%22isFreePlan%22%3Atrue%2C%22apiRoot%22%3A%22https%3A%5C%2F%5C%2Fcodesamplez.com%5C%2Fwp-json%5C%2F%22%2C%22apiNonce%22%3A%22155bc22a78%22%2C%22isPrivateSite%22%3Afalse%2C%22isWpcom%22%3Afalse%2C%22hasOverlayWidgets%22%3Afalse%2C%22widgets%22%3A%5B%5D%2C%22widgetsOutsideOverlay%22%3A%5B%5D%2C%22hasNonSearchWidgets%22%3Afalse%2C%22preventTrackingCookiesReset%22%3Afalse%7D"));
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://codesamplez.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