April 11th, 2008

Creating a Facebook Application using MVC

Facebook has been growing in popularity ever since it was released on February 4th 2004 at an almost unstoppable pace. Up until May 24th, 2007, it wasn’t much different than MySpace (or insert your favorite social network here), however on that day they rolled out a SDK that turned Facebook from a destination website to a platform that let any developer interact with their almost 71 million users. You can read more about the history of Facebook at Wikipedia.

My focus today isn’t on how to develop your first Facebook application in ASP.NET, because there are already many great articles on that, and even some starter kits. My focus is going to be on developing your first Facebook application with ASP.NET MVC, however this article will assume that you have the basic understand FBML (Facebook Meta Language) and MVC. If you do not have one or the other don’t worry, both are very easy to pick up on, and both have a very active developer community to answer questions.

So now that, that is out of the way lets start looking at what we need to make your MVC application in to a Facebook compatible application. The first thing you will need is the Facebook Developers Toolkit which is free on CodePlex. The second thing you will need is my Facebook MVC Web Controls which is a modification of the tookit’s Facebook.WebControls.dll made specifically for MVC. The third thing you need is ASP.NET MVC Preview 2 which is also available for free on CodePlex.

My tookit extension consists of the following classes, that mimic the current classes already in Facebook Developers Toolkit:

  • CanvasFbmlViewPage
  • CanvasFbmlViewPage<TViewData>
  • CanvasFbmlViewMasterPage
  • CanvasFbmlViewMasterPage<TViewData>
  • CanvasIFrameViewPage
  • CanvasIFrameViewPage<TViewData>
  • CanvasIFrameViewMasterPage
  • CanvasIFrameViewMasterPage<TViewData>

Facebook IFrame Application

I will start with the IFrame stuff since that is very easy and doesn’t require FBML knowledge. To create a Facebook IFrame application just follow the directions at Facebooks Getting Started Website for an IFrame. Then create an MVC Preview 2 application in Visual Studio. Then change the following in the CodeBehind for each of your pages.

public partial class Index : ViewPage

to

public partial class Index : CanvasIFrameViewPage

That is all you have to do to get Facebook working with your MVC application through IFrames. You don’t need to change your HTML because you site is going to render through an IFrame so there is no processing that is done in regards to UI rendering for Facebook. This has some drawbacks including not having the familiar Facebook interface, however this is the easiest way to get running on a Facebook app.

Facebook FBML Application

Creating the C# part of an FBML application for MVC is just as easy creating creating the IFrame application.

public partial class Index : ViewPage

to

public partial class Index : CanvasFbmlViewPage

Nothing spectacular there. Before we get started with the FBML application there is a tool that shows you what your FBML will look like when rendered out to HTML. However the real power of MVC is about to shine when we create a simple Facebook application using FBML and the ASP.NET MVC framework. Basically I took the default MVC application and modified the Index.aspx page to look like this.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="MvcFootprints.Views.Home.Index" %>
<div style="padding: 10px">
    <h2>Hi <fb:name firstnameonly="true" uid="<%= this.FBService.UserId %>" useyou="false" />!</h2>
    <form method="post" action="http://apps.facebook.com/coderjournal/">
        Get friend:
        <fb:friend-selector idname="friend" />
        <input value="find" type="submit" />
    </form>
    <hr />
    <fb:if-can-see uid="<%= Request.Forms["friend"] %>">
    <div style="clear: both; padding: 3px;">
        <fb:profile-pic style="float: left;" uid="<%= Request.Forms["friend"] %>" size="square" />
        <fb:name uid="<%= Request.Forms["friend"] %>" capitalize="true" />
    </div>
    </fb:if-can-see>
</div>

This isn’t very spectacular, all that it does it provide a friend list drop down, and submit it back to get their picture and name. But it gives the basic idea how to inner-mix FBML markup like <fb:name /%gl; and HTML with MVC. So basically that is your first Facebook application with MVC, nothing to it right?

That is really the beautiful thing about MVC, it makes writing simple applications that much simpler than ASP.NET Web Forms because you don’t have to deal with controls and it is encouraged to do processing inline with your markup. Happy coding. As always you can find the source code to this and my other projects in Coder Journals Source Control Repository.

Tags: , , , ,

Social: kick it on DotNetKicks.com | Shout it | Add to DZone

This entry was posted on Friday, April 11th, 2008 at 10:06 am and is filed under Programming. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed.

19 Responses to “Creating a Facebook Application using MVC”

  1. DotNetKicks.com Says:

    Creating Your First Facebook Application using MVC…

    You’ve been kicked (a good thing) – Trackback from DotNetKicks.com…

  2. Troy Goode Says:

    Very nice, Nick. I was just talking to Stephen Walther about this very thing last week, its good to see someone has gotten around to posting about how to do it.

  3. Nick Berardi Says:

    Hi Troy,

    Apparently you don’t even need base classes with Facebook.Net. http://www.marketing-ninja.com/?p=131 I am glad you enjoyed the article.

    Nick

  4. MyDotNetKicks Says:

    Creating a Facebook Application using MVC…

    You’ve been kicked (a good thing) – Trackback from MyDotNetKicks…

  5. WTF Apple, Show Some Common Curtsy - Nick Berardi’s Coder Journal Says:

    [...] Today I saw their was a QuickTime update while watching the Facebook Conferences presentation in anticipation of my previous post today. So as I decided to do the update, what a nightmare that has turned out to be. First of all it was [...]

  6. MVC + Facebook == Wonderful Development Platform - Nick Berardi’s Coder Journal Says:

    [...] my last post on the subject I was using the Facebook Developer Toolkit, however because of various implementation problems that [...]

  7. happy Says:

    hi there i am a newbie facebook developer i cant use the fbbl tag what could i be doing wron

  8. Nick Berardi Says:

    Why can’t you use FBML tags?

  9. happy Says:

    oops sorry and thanks was got it right

  10. happy Says:

    hey i need to find all the friends who installed my application. i have to show nam and picture.
    i am using asp.net
    any help will be appriciated

  11. brady gaster Says:

    I snagged your code and changed a few things, specifically (and only) bringing the code up to speed so that it works with the most recent drop of the MVC preview (5). I’m experiencing great success when I use the code to create an iFrame-based application. However, it appears as though the new Facebook API’s FBML application architecture breaks the way it all works together. Do you support the new FBML stuff or have any idea how this could be resolved, or is it on the side of Facebook with the decision to move forward with an API that breaks the previous approach? Any insight you could give on this would be appreciated. Thanks!

  12. Peter Says:

    hi nick…..
    I am using MVC Preview 5.
    I am tring to integrate Facebook single sign on in my MVC Preview 5 project.Can u please tell me is that possible?
    Is there any link or source code for that?

    Thanks in advance..

  13. Nick Berardi Says:

    Hi Peter,

    It is possible, I have done it on http://www.ideapipe.com/login, if you want to check it out.

  14. Nick Berardi Says:

    The second part of your question is basically you don’t need to do anything on your side, just follow the examples given by Facebook. And create a different page to post the results back to that will contain the Facebook UID.

  15. Peter Says:

    hi nick…..
    i gone through http://www.ideapipe.com/login.
    and the exactly same thing i want to implement in my project.
    Can u please tell me from where i get the examples for facebook?

  16. Craig Says:

    Hi Nick Berardi…..
    I am a newbie MVC Developer…..

    Can we use Google/Gmail Id for single sign on in MVC project?
    Which google apis or google services should MVC support?

    Any Help or Any suggestion…….

    thanks.

  17. Rahedideshacy Says:

    Apache/2.2.3 (Debian) mod_python/3.2.10 Python/2.4.4 PHP/5.2.0-8+etch15 mod_ssl/2.2.3 OpenSSL/0.9.8c Server at allhomedecor.org Port 80
    Not Found

    The requested URL /forxru/zadanie.txt was not found on this server.
    404 Not Found

  18. rhinarrateti Says:

    Apache/2.2.3 (Debian) mod_python/3.2.10 Python/2.4.4 PHP/5.2.0-8+etch15 mod_ssl/2.2.3 OpenSSL/0.9.8c Server at allhomedecor.org Port 80 The requested URL /forxru/zadanie.txt was not found on this server. Not Found 404 Not Found

  19. Neb Says:

    Hey
    I dont understand How you can have Codebehind in ASP.NET MVC?