• Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • Home
  • Featured
    • C# Tutorials
      • LinQ Tutorials
      • Facebook C# API Tutorials
    • PHP Tutorials
      • CodeIgniter Tutorials
    • Amazon AWS Tutorials
  • Categories
    • Programming
    • Development
    • Database
    • Web Server
    • Source Control
    • Management
    • Project
  • About
  • Write
  • Contact

CodeSamplez.com

Programming, Web development, Cloud Technologies

You are here: Home / Development / Using FQL With Facebook C# SDK

Using FQL With Facebook C# SDK

April 7, 2011 by Rana Ahsan 10 Comments

Facebook Api Tutorial In C#

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 🙂

Share If Liked

  • Click to share on Facebook (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Pinterest (Opens in new window)
  • Click to share on Reddit (Opens in new window)
  • Click to share on Tumblr (Opens in new window)
  • Click to share on Pocket (Opens in new window)

You may also like

Filed Under: Development Tagged With: c#, facebook

About Rana Ahsan

Rana is a passionate software engineer/Technology Enthusiast.
Github: ranacseruet

Reader Interactions

Comments

  1. andreauxyo says

    April 28, 2011 at 10:04 am

    where does the FacebookSession and CanvasAuthorizer come from?

    Reply
    • Rana says

      April 28, 2011 at 9:22 pm

      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.

      Reply
  2. Sriya says

    June 21, 2011 at 2:40 am

    The SDK does not contain a Query method that U’ve used. Where do I get the source for that?

    Reply
    • Rana says

      June 21, 2011 at 10:30 am

      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: http://codesamplez.com/development/c-sharp-library-for-facebook-api . Hope this helps.

      Reply
  3. Matt says

    July 19, 2011 at 10:32 pm

    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

    Reply
  4. robert says

    February 22, 2012 at 3:28 pm

    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?

    Reply
  5. India says

    January 19, 2013 at 2:46 am

    Awesome mate! Worked like a charm.

    Reply
  6. Narendra Jarad says

    June 26, 2013 at 1:52 am

    Awesome dear.thanks for sharing your idea.

    Reply
  7. Kinjal says

    September 19, 2013 at 8:56 am

    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.

    Reply
  8. architster says

    October 29, 2014 at 11:20 pm

    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

    Reply

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 3,774 other subscribers

Follow Us

  • Twitter
  • Facebook

Top Posts & Pages

  • How To Work With JSON In Node.js / JavaScript
    How To Work With JSON In Node.js / JavaScript
  • PHP HTML5 Video Streaming Tutorial
    PHP HTML5 Video Streaming Tutorial
  • How To Work With C# Serial Port Communication
    How To Work With C# Serial Port Communication
  • Facebook C# API Tutorials
    Facebook C# API Tutorials
  • Using Supervisord Web Interface And Plugin
    Using Supervisord Web Interface And Plugin
  • LinQ Query With Like Operator
    LinQ Query With Like Operator
  • Get Facebook C# Api Access Token
    Get Facebook C# Api Access Token
  • Getting Started With UDP Programming in Java
    Getting Started With UDP Programming in Java
  • How To Use Hotkeys/Keyboard Events In WPF Application Using C#
    How To Use Hotkeys/Keyboard Events In WPF Application Using C#
  • Control HTML5 Audio With Jquery
    Control HTML5 Audio With Jquery

Recent Posts

  • Building Auth With JWT – Part 2
  • Building Auth With JWT – Part 1
  • Document Your REST API Like A Pro
  • Understanding Golang Error Handling
  • Web Application Case Studies You Must Read

Tags

.net angularjs apache api audio auth authenticatin aws c# cloud server codeigniter deployment docker doctrine facebook git github golang htaccess html5 http javascript jwt linq mysql nodejs oop performance php phpmyadmin plugin process python regular expression scalability server smarty socket.io tfs tips unit-test utility web application wordpress wpf

Footer

Archives

Follow Us

  • Twitter
  • Facebook

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 3,774 other subscribers

Copyright © 2023