Developing Facebook applications in C# with ASP.NET

Updated VS.NET 2010 Facebook Starter Kit

I mentioned previously I’ve created my first Facebook (FB) application (which is still under development) but I’ve learned a lot along the way. I haven’t found the Facebook documentation very useful at all and have gleened a lot through Google, the Facebook developer group and simply tinkering with things. As a result I’ve put together some steps I think could help save you some time if you’re looking to write a Facebook application using ASP.NET and C#. Btw, you’ll get no help from regarding ASP.NET development.

One thing that took me awhile was understanding how an FB application appears on your profile page. I eventually realized that FBML can be used to push markup onto a users profile page. This block of markup can be set at the time you create a new application though the first time around, if you’re like me, this may see a bit confusing keep in mind I’m new new to FB itself so a lot of this was confusing. From the FB toolkit you can also use the method setFBML to programmatically set this block of markup. Needless, to say being used to MSDN documentation FB doc leaves a lot to be desired. That said there are a few tools you can use to play with the API as well as sample your FBML markup.

Facebook start app screenshot

At any rate, here are the steps I followed to get my application working. Note, the project includes support for Microsoft AJAX so you’ll want to have that installed as well although you could just tweak the .aspx and web.config files and use it without AJAX. The screenshot illustrates what you should get when your done. NOTE: In the following steps several links won’t work unless you have an FB account.

UPDATE: I’ve created a new page on my wiki with updated steps and a VS.NET starter kit to make this much easier.

  1. Sign up for a Facebook account
  2. Add the Developer application to your account (you need an account for that link to work)
  3. Click Developer application from the left column and select click Set Up New Application
  4. Enter the Application Name and expand Optional Fields
  5. Under Optional Fields
    1. Set your callback URL http://localhost… will work just fine for development
    2. Select “Use iframe”
    3. Click Yes for “Can your application be added on FaceBook”
  6. Under Installation Options
    1. Check “Developer Mode”
    2. Under Site Nav use the same URL from Canvas Page URL (see Optional Fields)
    3. If you use my template set Edit URL to your Canvas Page URL + /settings
  7. Click Save then from the Developers Application browse to My Applications
  8. Click View About Page then click Add Application. This will allow you to easily work with your application during development.
  9. Download the Facebook Developer Toolkit from Microsoft
  10. Download my C# ASP.NET app to get you started or generate the boilerplate code for handling login (sorry I don’t do VB.NET)
  11. You’ll need to add a reference to the Facebook assemby to this project as I didn’t want to include it as it being updated frequently right now
  12. Open Default.aspx.cs and fill in FACEBOOK_APPKEY and FACEBOOK_SECRET from your application’s page on Facebook.
  13. Fire up the browser and hit your<your_app_name> and you’re off!

Please let me know what problems you have or let me know if these steps need tweaking.

Was this helpful?

[Updated: June 11, 2007] Added link to an update page on my wiki with this content.

[Updated: Feb. 24, 2008] Since this post was written I’ve created two VS.NET starter kits, one for Facebook Dev Toolkit and Facebook.NET. Here are some additional links:

50 thoughts on “Developing Facebook applications in C# with ASP.NET

  1. Can you elaborate which tools you used during the process? BDS 2006? Or is there some "Facebook IDE" you can develop in. I never heard of it, so your blog post is rather interesting to consume new information on the topic…

  2. Facebook is agnostic as long as your tool of choice can handle REST though from their web site they promote PHP and Java with minor mentions of other frameworks. I’ll avoid the religious debate on tooling as I don’t believe in religion when selecting development tools. My current favorite happens to be Delphi from CodeGear though YMMV.

  3. Hi Steve,
    First of all, thanks for sharing your experience!
    I tried to run your application acc to your instructions, but for some reason the method "GetUserInfo" returns the following error:
    The underlying connection was closed: An unexpected error occurred on a receive.
    Do you have any suggestion what I should do?
    Thanks again,

  4. hi
    I am new to Facebook…I developed desktop application in .Net
    i am getting the Friendslist,i used the following code in page_Load,
    friendList1.Friends = facebookService1.GetFriends();
    I am getting error: NullvalueExceptionHandled
    Object Reference not set to be an instance of an object
    how to use my application in Facebook…

  5. Thanks for helping us. This is great article. Now I like to add Profile box via, C#. I could not find any sample code for it. How can I design profile box via Do you have any sample project? Thansk.

  6. Young,
    I’m not in a position where I can provide free Facebook technical support. If you’re interested in consulting please let me know and I’d be glad to help otherwise, I’d suggest the Facebook Developer forums.

  7. What are your experiences with FBML and controls?
    I’ve been tinkering also and have been finding it somewhat difficult to debug some issue partiularly when it comes to webcontrols. Some work others do not.
    I’m guessing its all related to the FB version of javascript and its attempts to create javascript namespaces but have yet to come to any concrete conclusions on what works and what doesn’t and workarounds etc.

  8. I am creating application using in iFram mode.What i have a problem is when data grow a ugly scroll bar appear on right side of application. What i want is to hide those ugly scroll bars that get with an Application.

  9. I’m sorry, I don’t know what you mean to do with this code. Are we supposed to create an empty project in Visual Studio Express and put your code in it?

  10. rocky,
    In an IFRAME implementation you can’t use FBML so the multi-friend selector isn’t an option, you’ll need to implement that on your own. For more help, I’d recommend the Facebook developer forums.

  11. Hello,
    I am developing a facebook application on .net technology to do character analysis.Once the survey for this is done, the application result needs to be appeared in the profile page.I tried html iframe, <fb:iframe, <fb:ref etc…But Nothing worked out.
    I know there is a technique to display dynamic contents in profile page from an external URL..
    Please reply as soon as possible..
    Its Very Urgent…
    Fabeena Thoufeeq

  12. Hi,
    I am trying to developed a local facebook application.I had also created one application in facebook and also set a canvas url as
    But when go thought this url hall facebook login page is display in canvas.Whenever i am login for my page that time canvas page is show me logged but outer page not.I have also screen shot for that but not able to send you.
    Soni Umesh

  13. Thanks for being such a big help for everyone Steve.
    I have a question about testing Facebook Apps. I have already deployed my facebook app so in my Facebook settings, I have a live Callback URL.
    When I make updates to the program and I want to test them locally on my machine, I need to change the Callback URL to http://localhost… but that breaks the application for all of the ‘real’ users while I test.
    Is there a good way around this? Thanks!

  14. Hi Phil,
    Thanks for the kind words. I think there are a few things you can do including creating another application on Facebook that simply uses a different callback URL. Additionally, I’d recommend designing your application in such a way that the portions that don’t require access to Facebook can function without that requirement allowing you to test those features in isolation. Your question leads me to believe that it would be valuable for frameworks such as Facebook Dev Toolkit and Facebook.NET to provide a mock interface which would more easily allow for testing outside of the Facebook server (perhaps something already exists). I’m sure something like that could be done and in fact sounds like an interesting project.

  15. Hi Steve,
    Are there existing applications in facebook created in ASP.Net? If so, can you name some of them? So that I can test if apps built in ASP.Net is as stabel or as fast compared to other platforms.

  16. Hi Paul,
    Since Facebook applications run in an IFRAME it’s less obvious they’re ASP.NET apps. It’s entirely possible some FB apps you’ve used are ASP.NET apps. Is it really necessary to demonstrate that ASP.NET is fast and stable when running as an FB app when MS, Dell, MySpace and many other major sites all use it?
    Personally, I don’t think it’s an issue. If you’re really feel your FB app is going to scale to such size as to raise concerns about the scalability of ASP.NET I’d say you should focus on other areas first like how you’re going to write such a scalable app in the first place. Next, I’d worry about the hosting provider and whether or not they can supply you with scalability, sufficient bandwidth, load balancing, failover etc. Now, to me those would be real concerns for a wildly successful app.

  17. I have a problem with Ajax in my ASP.NET application.
    I add AjaxToolKit to my app and use UpdatePanel on default page.
    I recive JavaScript Errors:
    Line: 41
    Error: ‘Sys’ is undefined

  18. Hi Steve,
    Is there any new .net toolkit version covers the changes in Facebook API?
    Facebook will launch the new version soon. I think the current toolkit may not work correctly with the new changes.

  19. Hi Mido,
    No, at least not that I’m aware of. Unfortunately, I too have major concerns about the Facebook Dev Toolkit. I’ve attempted to contact one of the developers through the Codeplex connection and got no response. It’s beginning to look like Facebook development in ASP.NET is at a bit of a standstill which is really unfortunate. That said, we do have the source to this library as well as Facebook.NET so perhaps it’s up the to community to grab the reins. I wish I had time the time to do just that but that’s not in the cards are this point.

  20. I have a question, Is there a way to make an application to only be called from your website? I have an app that sends mini feeds to the user’s friends. When the user installs the apps, he can call it from his facebook account via link. My issue is that when the app is being called from the user’s facebook account, the web page that is inside the facebook frame just don’t look right(too big for the frame). When the app is being called from my website the page is not being pulled into the facebook frame and it looks perfect. How can I fix this issue, is there a way to fix it? If I have to create a specific page for each of the mentioned calling methods, how can I identify where the app is being called from from my website or from the user’s facebook account link of my installed app.

  21. hi
    i just want to ask can i login to my site using facebook account (name and password)

  22. Oleg & miro,
    For support on Facebook development I’d highly recommend the Facebook developer support forums. That said I’m not really sure I understand either question being asked.

  23. Steve,
    You blog was the starting point for me when I first got into Facebook development. The information you have here is GREAT!
    At the same time I’ve been reading Applying Domain-Driven Design and Patterns by Jimmy Nilsson. And have also purchased, and use Martin Folwer’s book on Patterns in Enterprise Application Architecture. In addition, I’ve worked a bit with Castle’s ActiveRecord & Monorail.
    That brings me to how I’ve changed my approach to developing Web Application and see a lot of value in the Model View Controller pattern.
    I therefore built a Add-on for the Facebook Developer Toolkit that adds support for ASP.NET MVC Preview (unfortunately not Castle Monorail although I may add it later)
    It’s a little lean on documentation but I’d love for you to check it out and let me know what you think…
    Thank for the great info! Peter

  24. Steve,
    thanks for your answer but I still can’t find what I am searching about which is getting information from facebook without entering the login page just sending request containing e-mail and password and get a responce , can that be ?
    I worked in the steps put i stopped at the exception saying "invalid parameter " when i call the function CreateSession(AuthToken) !!!!!!!!!!
    so I need to know what to do with that exception

  25. miro,
    I don’t understand what it is that you’re trying to do. It sounds like you’re attempting to circumvent the login page and running into trouble which doesn’t surprise me.
    Good luck.

  26. How can i know if a user add my application to his application.
    How can i update feeds, like user has scored the heighest score etc…

  27. Hi Steve,
    Its been great challenge to work on face book API. I am trying to send a mail through Notification.SendEmail method. The method returns NULL with no error message/exception as per the documentation this method returns coma seperated mailids to which the mail is sent successfully.
    Can you please help me to resolve this issue? Its very urgent.
    Thanks a lot in advance.

  28. Hi steve,
    Iam new bie to facebook developement…
    when i get the assignment on last month, iam used the starter kit. it is very helpful for me. but after facebook turned to ‘NEW’ my application running in trouble. so i downloaded new toolkit from codeplex ver.2, there are lot of changes in the codebase, so can you please update the starter kit ? it will really helpful for the new developers like me. Thanks.

  29. hi have just starter developing facebook app, i am newbie have tried to use friendlist control to show list of friends in my source file after ispostback i used frindlist.friends = fb.getfriends but i can not see any of my firnends when the app start any idea

  30. Hi
    I am new to facebook application, now i created a application in facebook, after that created a website in visual studio 2005, my task is to send a message or update status in facebook user. I downloaded the .Net Facebook API Client Alpha v2.0 but this starter kit is not responding well, it means that, when ever I opened the Facebook.VisualStudio.exe it is open a starter kit asking to enter api key and secret key after giving values i press continue after that it is not populating why ……
    Please tell me what wrong i am doing
    Thanks & Regards

  31. Hi Steve,
    Yes its a nice post. Since the documentation for not so good yet thats why your tricks will help us a lot. Thanks for sharing.

  32. Hi,
    I am developing windows application in i.e uploading photos and videos to the facebook using face book api’s. I am able to loging, getting albums but i need to upload photos and videos to the face book. Please help me to come across with this. I am using
    " facebookService1.CreateAlbum("name","image path","test");"
    can you suggest me how to resolve this.

  33. Hi steve,
    I am new to facebook application, now i am developing application using technology for desktop application to upload photos to the facebook. I am able to create album using facebook api but not able to upload the photos to the it asking for aid(album id). Can you please tell me how to get the album id.
    Thanks & Regards,

  34. Dear Sir,
    I have developed a desktop application using C# .Net. Now I want to make a Facebook app out of this with similar functionality. How could I best do this in the shortest possible time.
    I would eagerly for your kind suggestions and reply.
    Best Regards,
    Jagmohan Singh

  35. Hi,
    I am using Notifications.SendEmail(userId, "text","msg",""). But it returns null. How to proceed?
    Thanks in advance

  36. Thanks Steve!
    Tried lots of different facebook start kits /tutorials in last 4 days, your article & startkit v2.1 got us up an running in minutes!!

  37. excellent – i used your sample app and i was up and running building a facebook app for my compay in no time.
    ill keep you posted with my next facebook venture

Comments are closed.