“Hello, World” FBML app, with Postback, using Toolkit 2.0

Apr 15, 2009 at 7:46 AM
Edited Jan 24, 2011 at 11:58 PM



~ javaman

I recently spend more than a week getting a basic FBML app working with Facebook Toolkit 2.0 (Nov 08).

The instructions with the toolkit documentation were good enough for getting started, but it still took quite a while to get past basic problems.

Some issues:
-    My web host only accepts ASP.NET “web sites”, not “web projects”. The Toolkit doco and sites are oriented to projects. I was never able to compile the Toolkit FBML sample (FBMLCanvasSample).
-    I ran the Toolkit FBML sample in Facebook. They don’t publicise it, but it’s available at http://apps.facebook.com/keep-smiling/. It mostly works, which was reassuring.
-    I hit the several of the problems mentioned in these forums, mainly the InvalidOperationException: "RegisterForEventValidation… , Master.API not being visible from the Content, and Master.API.UID = 0. (these were where most of the time went!)

As I haven’t seen a complete, simple solution to these problems, I thought I’d post it here.

1. Getting started.

At your host: Create an ASP.NET “web site”, with a url which you’ll use as the callback in Facebook. You will probably need to pay for a commercial ASP.NET host, and a new domain name. I used www.discountasp.net.

In Codeplex: Download the toolkit, and unzip it (it doesn’t matter where).
In Facebook: Create a Facebook app, with app key and secret, as described in several introductory tutorials on the web.

In Visual Studio:

2. Create a new ASP.NET web site

3. Add a “Bin” folder to the site (“Add ASP.Net Folder”), and copy the “Binaries” files (Facebook.dll) etc to the “Bin” folder, and then use “Add existing item” to include them in your project.

4. Edit “web.config”, and change the empty <appsettings/> entry to:

    <add key="APIKey" value="<your API key – from facebook"/>
    <add key="Secret" value="<your API secret – from facebook"/>
    <add key="Callback" value="http://www.yourhost.com/yourapp/"/>
    <add key="Suffix" value="yourapp"/>

Note: there are several other settings (eg. TemplateID1) in the samples. You may need these later, but they aren’t necessary for the basic app. I’ll leave them out here.

5. Use these four files (in addition to web.config and the Bin files)


<%@ Page Language="C#" AutoEventWireup="true" MasterPageFile="~/MasterPage.Master"
    CodeFile="Default.aspx.cs" Inherits="_Default" %>

<%@ MasterType VirtualPath="~/MasterPage.Master" %>

<asp:Content runat="server" ID="content" ContentPlaceHolderID="body">
        <asp:label runat="server" id="Label1" text="Label"></asp:label>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:button id="Button1" runat="server" text="Publish!" onclick="Button1_Click" />
    <!-- Need this for user to add box to profile. Otherwise, SetFBML just does nothing -->
    <fb:if-section-not-added section="profile">
            <div class="section-button"><fb:add-section-button section="profile" /></div>
            <p style="font-weight:bold">Press this button to add this app to your profile Wall</p>

Note: The “MasterType” directive is needed to access the MasterPage class in the code behind for the content.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page

    protected void Button1_Click(object sender, EventArgs e)
        Label1.Text = "Bingo!!!";
        Master.API.profile.setFBML(0L, TextBox1.Text, TextBox1.Text, "");

    protected void Page_Load(object sender, EventArgs e)
        Label1.Text = "Greetings, " + Master.API.users.getInfo().first_name + ", Your UID = " + Master.API.uid;



<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %>

<asp:label runat="server" id="header">
<fb:tab-item title="Sample Tab1"  href="default.aspx" selected="<%=Convert.ToInt32(true) %>"/>
<fb:tab-item title="Sample Tab2"  href="default.aspx" selected="<%=Convert.ToInt32(false) %>" />
<div id="main_body">
    <!-- Need a form to enable postbacks. This works, ie. in the MasterPage, around the content. -->
    <form runat="server" id="form1">
    <asp:contentplaceholder id="body" runat="server">


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class MasterPage : facebook.web.CanvasFBMLMasterPage
    // Need this to get a valid values for Master.API.UID and Master.API.users in the
    // content pages.
    public MasterPage()
        : base()
        RequireLogin = true;

    // Need this to overcome bug in the toolkit which causes "InvalidOperationException: "RegisterForEventValidation ..."
    // when rendering a button.
    protected override void OnPreRender(EventArgs e)
        var sw = new System.IO.StringWriter();
        // var htmlTW = new HtmlTextWriter(sw);
        // this.RenderControl(htmlTW);


6. Deploy to your host, and run the app within facebook.

Apr 15, 2009 at 9:16 AM
Edited Apr 15, 2009 at 10:07 AM
Oh, what does it do?

It presents a Canvas page, with "Greetings, <first name>, your UID is <uid>" (_Default.Page_Load)

It presents a textbox and a button. The user enters some text and presses teh button. The text is published to the user's wall in their facebook profile. (_Default.Button1_Click)

Try it out: http://apps.facebook.com/jmanhworld
Apr 30, 2009 at 12:44 PM
This was a really super example.  I'm teaching a class where the output will be a facebook application; and your hello world walk through and code was much easier and eliminated some spurious errors i got using the fbmlcanvas sample from the toolkit.  thanks!
Apr 30, 2009 at 6:35 PM
Edited Apr 30, 2009 at 7:49 PM
sorry, post on wrong forum
May 2, 2009 at 1:10 AM
Hey, thanks ToddW! I'm so glad that my post was able to help you and your class, and thanks for responding here.
May 2, 2009 at 2:55 AM
Somebody owes you money. A lot of money.
May 2, 2009 at 4:19 AM
You should be the director of microsoft's...microsoft. You should be the director of microsoft. THANK YOU!!
May 2, 2009 at 5:50 PM
I'm able to run my application at http://apps.facebook.com/nashvillevideo, I'm able to post content via my app to the wall, and the boxes tab, but, when I click the tab for my application, I get this error:
fb:redirect: redirect forbidden by flavor TabFBMLFlavor
Any ideas. I've looked the the toolkit for a way to change require_login, as other posts suggest, but can't find where that method is called. Not really sure that's the problem anyway.
May 3, 2009 at 5:17 PM
Require login is the problem - when set to false, everything is fine when you're logged in and looking at the app from a tab, but it creates problems in other scenarios. Need to be able to distinguish when user is on tab or elsewhere, or, need to implement the fix mentinoed in other place for the TabFBMLFlavor issue. I don't know how to do that though.
May 4, 2009 at 2:04 AM
Hi jypelton,

First off, thanks for your kind words about the sample :)

I didn't test the app from a facebook tab. Thanks for reporting the problem (and investigating it!).

You are correct that setting "RequireLogin = false" (in MasterPage.Master.cs) gets past the "redirect forbidden by flavor TabFBMLFlavor" problem. When I do that, then I get the next problem...
FBML Error (line 18): illegal tag "body" under "fb:tab-position"

I've had a quick look on the web, and it looks like facebook is objecting to finding a "body" tag in a tab (http://forum.developers.facebook.com/viewtopic.php?id=30564). I am only a little bit familiar with Facebook tabs, but my understanding is that you should actually create a different page for the tab (ie. call a different URL from the canvas URL), because the behaviour of the tab is different from the canvas.

If you use a different URL for the tab callback, then this gets around the problem of knowing whether you are being called from a tab or not.

I'll look into it in the next week or so, and post back here if I fix it.
May 4, 2009 at 5:27 PM

I am using your code as you presented it in the Forum but I keep getting this message.


Error while loading page from CheckItOff

There are still a few kinks Facebook and the makers of CheckItOff are trying to iron out. We appreciate your patience as we try to fix these issues. Your problem has been logged - if it persists, please come back in a few days. Thanks!

 I also tried one without the masterpage and content tags with just "hello world".

Any idea where I should look?

May 4, 2009 at 8:05 PM
this is a generic error. Try looking at your cavas page url configuration ( is there a "/" at the end of your url or "/page.aspx"). Next,  take a look at your aspx pages, make sure that are exactly right - exactly as they are in the sample, until you get it working. If nothing else, put a plain text html page up and try to get that working first.
May 13, 2009 at 2:51 AM
Edited May 13, 2009 at 2:54 AM

This is strange. I had been able to, I thought, publish a short story to my wall, but now nothings happening. Mabye my memory is failing me. Is anyone else having success punlishing to the wall with this example? I can publish to the 'box' on my wall, on the left hand side of the page, but not in the main center area, where all the updates are. This the case when useing my deployed version, or javaman59s original app at http://apps.facebook.com/jmanhworld

May 13, 2009 at 6:27 AM
Edited May 14, 2009 at 3:39 PM

@jypleton: This "Hello World" sample only publishes to the box. To publish a story to your wall you need to publish a user story. This is described in various recent threads in this forum (http://facebooktoolkit.codeplex.com/Thread/View.aspx?ThreadId=54798). I need to do this myself anyway, so I'll do it with the "Hello World" first, and post back here (probably tomorrow).

May 14, 2009 at 3:17 PM
Edited Jun 22, 2009 at 11:38 AM

I've got it going.

It's based on the "Setup" web form in the toolkit FBML Canvas sample, and also a good post by geograd (http://facebooktoolkit.codeplex.com/Thread/View.aspx?ThreadId=46683).

15/5/09 - edited to use the minimal code to produce a feed. Notable changes are that in Setup.aspx.cs require_login is now "false", and in Default.aspx.cs I've replaced the "friends" parameter with "null". The "friends" parameter is only relevant if there are friend {target} keys in the template, eg. "{actor} gave {target} a hug"

23/06/09 - This solution only produces a minimal news feed - it puts a brief entry on the user's wall. It doesn't send one of those cool news items to friends' news feeds. I hope that a similar structure can be used for that too (but I'm not sure).

You can see it working  at http://apps.facebook.com/jmanhworld.

1. Create a web form called "Setup" in the top level of the app.

Put this code in the Setup.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Setup.aspx.cs" Inherits="Setup"
    Debug="true" Trace="true" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<asp:label runat="server" id="label1"></asp:label>

Put this code in the Setup.aspx.cs file:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using facebook.web;
using facebook;

public partial class Setup : CanvasFBMLBasePage
    protected void Page_PreInit(object sender, EventArgs e)
        this.RequireLogin = false;

    protected void Page_Load(object sender, EventArgs e)
        // We'll just define a one line story.
        var oneLineStory = new List<string> { "{*actor*} says \"{*todays_msg*}\" today" };

        // Set up stubs for the short story, and full story.
        // For an example of using these, see Setup.aspx.cs in the toolkit FBMLCanvasSample.     
        var shortStoryStub = new List<feedTemplate>();
        var fullStoryStub = new feedTemplate();  // Full stories are no longer published: http://wiki.developers.facebook.com/index.php/Stream_Release_Notes

        var templateBundleId = this.API.feed.registerTemplateBundle(oneLineStory, shortStoryStub, fullStoryStub);
        label1.Text = "Your new templateBundleId is " + templateBundleId.ToString();


2. Log into facebook, and run the Setup file, with the URL


It will display the template bundle ID:

eg. Your new templateBundleId is 84270922995

3. In Default.aspx.cs Button1 handler, write to the feed...

   ... using facebook;

   protected void Button1_Click(object sender, EventArgs e)
        Label1.Text = "Bingo!!!";
        Master.API.profile.setFBML(0L, TextBox1.Text, TextBox1.Text, "");

        // Publish news feed
        var templateData = new Dictionary<string, string>();
        templateData.Add("actor", Master.API.users.getInfo().first_name);
        templateData.Add("todays_msg", TextBox1.Text);
        Master.API.feed.publishUserAction(<template_bundle_id>, templateData, null, "", feed.PublishedStorySize.OneLine);
4. Run the app. Enter some text in the box, and press "Publish". (as before)

5. Go to your Profile, and see the message in the box, and on your wall.


May 14, 2009 at 3:45 PM
Edited May 14, 2009 at 3:45 PM

Looks good. I was stumbling on the bundleID/templateID issue.  We should be able to rate posts or users on here. You get 5 stars.

May 14, 2009 at 8:16 PM

Well, I got the setup.aspx file to run and got my templateID, but no luck with default.aspx. I'm getting an error for feed.PublishedStorySize.OneLine. In fact, intellisense does not recognize feed. VS says the feed is unrecognized in this context (Something to that effect). Been looking for other ways to write this, but no go yet.

May 14, 2009 at 8:31 PM

IT WORKS!!! I haven't been this happy since I saw my first line of HTML rendered in a browser. There were two problems. I was treating <my_template_bundle_id> as a string, so in qoutes, which it should not be in qoutes. Secondly, I had to ad "facebook" before "feed" as in facebook.feed.PublishedStorySize.OneLine

Once again, thank you javaman59!

May 15, 2009 at 11:29 AM

Thanks for encouragement, jypelton. It helps a lot!

I've simplified the instructions, and added a "using facebook" to the default.aspx.cs, based on your feedback. I think that intellisense would normally pick up the "string" vs "long" issue for <my_template_bundle_id>.

I'm wondering whether this is really giving us what we want though. It only publishes to the user's wall, not to their friends' news feeds, so will only be visible when friends browse our profiles. The facebook instructions are very confusing about this - they seem to say that only short stories (as opposed to one-line stories) are published in friends' news feeds, but "rich" media content (ie. images and video) are more likely to be published. Also, "feed forms" (whatever they are!) are preferred. http://developers.facebook.com/news.php?blog=1&story=206

Anyway, it is only a "hello world" app, so I'll have to leave it there.

May 24, 2009 at 3:53 AM

I have my first Facebook app working based on the Dev Toolkit 2.0.

Similar to your sample above I am successfully posting to the profile, however, I am having a problem where I cannot get it to post short story items. Even if I specify feed.PublishedStorySize.Short in the publishUserAction call, it still only posts a one-line entry to my page.

Facebook's documentation says one-line entries are only published to the user's Wall, not the Homepage news feed which other users see. To do that, it says you need to publish a short story.

But when I try short story, it still posts a one-line.

I checked my app settings on Profile/Applications but the only option available for my (or any) application is a checkbox that says "allow this application to post one-line stories to my profile".

Is there a bug in the Developer Toolkit that is preventing publishing a short story?

<font size="2">

API.feed.publishUserAction([registered templateID here],


Nothing, Nothing, "", feed.PublishedStorySize.Short)

May 24, 2009 at 5:49 AM

OK I figured out why it wouldn't post short stories... you have to have the user grant your app extended permissions first as described at this link:



Jun 21, 2009 at 7:35 AM

Has anyone figured out the code yet to post a short story to self or a friend? The code above works fine for a one line story and as willmckee pointed out you need extended permissions first for a short story. However the code above does not define a short story in either setup.aspx.cs or defaults.aspx.cs. Has anyone managed to successfully add the short story code to both of these files?

Jun 22, 2009 at 11:14 AM


I want to know how you have implemented Enable Email functionality in Smiley application. Please explore on that.

I also want to implement the same. Kindly help.



Jun 22, 2009 at 11:32 AM
Edited Feb 9, 2010 at 4:07 AM

Sorry, tushar, I can’t help you with that. I haven’t enabled e-mail functionality.

It’s in the Smiley app code, in the files sendsmiley.aspx and sendsmiley.aspx.cs (you can find this in the toolkit, under FBMLCanvasSample). You can try and run it in facebook by going to http://apps.new.facebook.com/keep-smilingmysmilies.php, but when I press Send Smiley I get an application error.

Good luck with it!


Jun 22, 2009 at 11:40 AM

Thanks a lot for your help Javaman.

Jan 24, 2011 at 11:09 PM

I have a simple html page and still getting the same error, i dont have a master page or code behind, i just want to be able to see my rendered page in a page tab in facebook ? any ideas ?

I found some posts say that my html page shouldn't have body  tag and i did this and still i have the same problem.


my page url :


my facebook page tab url:



any help is appreciated ?




Jan 24, 2011 at 11:55 PM

Sorry marafar. This is all just too long ago for me to be able to revisit it, and look into your problem.

I haven't looked at this app since facebook changed their API in August, 2010.

BTW, if you want to see what does work, and what does not work, in the app, it is still there at


When you run the app, you will see a note I made in August 2010.

I strongly recommend that you get the latest toolkit, (3.0?), and also use the latest facebook API, which has changed significantly since I wrote this app.


~ Javaman