More in this section
Blogs RSS feed

Facebook, C# Sitefinity Integration

by User Not Found

Facebook offers a C# API that allows you to display a friend list on your website. To implement this within Sitefinity, you must obtain a Facebook user account, obtain a Facebook API Key, download the Facebook SDK, bind a Repeater to your friend list, and upload this functionality as a user control, or create an external page as shown in this article, to Sitefinity.


Getting Started

Here is what you need to get started:

1. First, you must obtain a Facebook user account and add some friends. This can be done on Facebook's website.

2. Once that is complete, you must obtain an Developer API and Secret key by going to Facebook's developer website API Key generator. If you have not logged in, you will be asked for your user name and password prior to getting the key generated. Add the name of your application to that form. You will then see a the API and Secret Key on the next screen.

3. Download the Facebook SDK and place all of the files in the bin folder of your application.

You application should look like this:




Authenticating to Facebook

Application users must login to Facebook before using an application. In your default.aspx page, as shown above, you must create a method to authenticate your user as shown below:


/// <summary>
    /// Returns the user's authentication token
    /// </summary>
    /// <returns></returns>
    private facebook.Components.FacebookService AuthenticateMe() 
        facebook.Components.FacebookService _fbService = new facebook.Components.FacebookService(); 
        // ApplicationKey and Secret are acquired when you sign up for an 
        _fbService.ApplicationKey = ConfigurationManager.AppSettings.Get("APIKey"); 
        _fbService.Secret = ConfigurationManager.AppSettings.Get("Secret"); 
        _fbService.IsDesktopApplication = false
        string sessionKey = Session["Facebook_session_key"as String; 
        // When the user uses the Facebook login page, the redirect back
        // here will have the auth_token in the query params
        string authToken = Request.QueryString["auth_token"]; 
        // We have already established a session on behalf of this user
        if (!String.IsNullOrEmpty(sessionKey)) 
            _fbService.SessionKey = sessionKey; 
            _fbService.uid = (long)Session["Facebook_userId"]; 
        // This will be executed when Facebook login redirects to our page
        elseif (!String.IsNullOrEmpty(authToken)) 
            Session["Facebook_session_key"] = _fbService.SessionKey; 
            Session["Facebook_userId"] = _fbService.uid; 
            Session["Facebook_session_expires"] = _fbService.SessionExpires; 
        // Need to login
            Response.Redirect(@"" + _fbService.ApplicationKey + @"&v=1.0\"); 
        return _fbService; 

In your Web.config, you must provide the API, Secret Key and Callback URL for authentication:


        <add key="APIKey"value="xxxxx"></add>
        <add key="Secret"value="xxxxx"></add>
        <add key="Callback"value="http://localhost/test/facebook/"></add>


The Callback is where your user goes after he or she authenticates to Facebook using your application. In this case, the value is the location of my virtual directory for this project.


Binding the Data

I will bind a repeater to my Facebook buddy list. To shorten the list of friends, I will create a buddy list. This is a subset of friends on my Facebook profile. I will use Boyan and Ivan who are Sitefinity bloggers.

To create the list, follow these steps:

1. Login to your Facebook account

2. On the top navigation, click Friends

3. Click Create New List

4. To add friends, click Edit this List

5. Follow the on-screen instructions

6. From the query string, copy and paste the list ID as shown below in bold: 

7. Bind a Repeater called Repeater1 to the data as shown below:



using facebook.web; 

protectedvoid Page_Load(object sender, EventArgs e) 
        facebook.Components.FacebookService _fbService = AuthenticateMe(); 
        if (!IsPostBack) 
            // Bind a repeater to a list of friends, based on a special friend list
            //Remove anything in between .getUserObjects() to get a list of all friends for a user
            Repeater1.DataSource = _fbService.friends.getUserObjects(572701275029); 
            //To get a list of fields, get each user instance as shown below
            //foreach (user u in _fbService.friends.getUserObjects())




        <asp:Repeater ID="Repeater1"runat="server"
        <asp:Image ID="Image1"runat="server"ImageUrl='<%# DataBinder.Eval(Container.DataItem, "pic_big") %>'/>
        <br />
        <asp:HyperLink ID="Name"runat="server"Text='<%# DataBinder.Eval(Container.DataItem, "Name") %>'NavigateUrl='<%# Eval("UID", "{0}&amp;ref=profile") %>'Target="_blank"/>
        <br />
        <br />


Once code has been implemented, your end result would look something like this:




Sitefinity Integration

From here, you could create an External Page from the default.aspx file. For more info, please read Working with Pages > External Pages in our User Manual. You could also copy and paste the page's contents into a user control and upload it to Sitefinity.



**Sample Code Available Here**


Leave a comment
  1. Gert Hofstra May 18, 2009
    Great article, Joe!
  2. Joseph Anderson May 18, 2009
  3. Gray.Blog Jun 02, 2009
    Pingback from:
  4. Steve H Jul 02, 2009
    Thanks for all of your help Joe! This is really nice! Great post!

    There is another .NET Facebook article I have seen in a recent magazine lately (ASP.NET PRO?). I'll play around with this when I have time.
  5. Ershad Aug 14, 2009
    nice article joe, Thank you
  6. Siju George Sep 17, 2009
    Nice and Clean yaar. Good work!!!
  7. Harsha Oct 21, 2009
    fbservice.CreateSession (authToken) not working raising an exception "Connection closed and unknown exception occured can not send". Help me to sort this out.
  8. Jeremy Aug 26, 2010
    Awesome, thanks!
  9. priyanka Sep 27, 2010
    Really...Thanks ...

    Leave a comment