Build Facebook Applications with Silverlight 3 and Silverlight 4 - part 2

[Infragistics] Mihail Mateev / Friday, April 16, 2010
In this article I will demonstrate how to build Silverlight applications using the latest Facebook Developer Kit for ASP.Net from scratch
In this article I will demonstrate how to build Silverlight applications using the latest Facebook Developer Kit for ASP.Net
There are three possible ways to build Silverlight applications with Facebook Developer Kit
  1. Build a Silverlight application, hosted in ASP.Net application, that use Facebook API for ASP.Net . This approach is very similar to described in part 1 of this article: Build  a Facebook ASP.Net application using Facebook Starter Kit. 
  2. Build a Silverlight application using Facebook API for Silverlight
  3. Build a Silverlight out-of-browser application for Facebook

Part 2 of this article describes how to build a Silverlight application that use Facebook API for ASP.Net .

Part 3 will cover Silverlight applications that use Facebook API for Silverlight.

 All examples was built with Visual Studio 2010, .Net 4.0, Silverlight 4.0 and Facebook Developer Kit version 3.02

Configure an ASP.Net Application, that hosted Silverlight Content

 In this article is used the latest release of the Facebook Developer Toolkit Version 3.02 http://facebooktoolkit.codeplex.com/

Assemblies are built with VS 2008 and .Net 3.5 / Silverlight 3.0
It is not a problem to rebuild the source  with VS 2010 and .Net 4.0 / Silverlight 4.0
Only for Facebook.Web.Mvc solution you must install the latest version of Microsoft ASP.NET MVC 2 RTM http://aspnet.codeplex.com/releases/view/41742
 

 There are several steps to create a Silverlight Facebook AS application using Facebook Developer Kit and API for ASP.Net  

  1. Create an ASP.Net application.
  2. Add Reverences to Facebook ASP.Net libraries.
  3. Add a cross-domain communication file.
  4.  Set the static port for our Web Application.
  5. Create a new Facebook application.
  6. Add APIKey and Secret to Web.config of your Web Application.
  7.  Change the content of the Default page.
  8. Change the content of the master site  page.
  9. Add your SIlverlight application in the solution.
  10. Integrate the Silverlight content in the ASP.Net application
Create a new ASP.Net Application with VS 2010
Add Facebook.dll and Facebook.Web.dll references to your ASP.Net application.

In order for the Web application to communicate with the Facebook platform, a cross-domain communications channel file must be created and added to the Web application. This file references the JavaScript cross-domain library provided by Facebook which establishes and handles the transfers between Facebook and third-party Web sites.

 More information about Cross Domain Communication you can find there: http://wiki.developers.facebook.com/index.php/Cross_Domain_Communication
 Add a new HTML page to your of the Web application named "xd_receiver.htm"
Open xd_receiver.htm and replace all markup with the following content:
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Cross-Domain Receiver Page</title>
</head>
<body>
     <script src="http://static.ak.facebook.com/js/api_lib/v0.4/XdCommReceiver.js?2" type="text/javascript"></script>
 </body>
</html>
 
Set the static port for our Web Application and save the project.
Create a new Facebook application.
Copy APIKey and Secret from Basic tab to use it your application.
 
Set a local application URL in Canvas tab -> Canvas Callback URL
Set a canvas page  URL in Canvas tab -> Canvas Page URL
Set IFrame Size to be “Resizable” 
 Save the Facebook Application.
 Add APIKey and Secret to Web.config of your Web Application:
 <appSettings>
  <add key="APIKey" value="0df0965cab2684a029288805523b2596"/>
  <add key="Secret" value="929b3f5d1ef7af0b01149151e2658eab"/>
</appSettings>

In Default.aspx page:

Remove the content inside <asp:Content></asp:Content> tags.

Add  the new   content of the Default.aspx:

<asp:Content runat="server" ContentPlaceHolderID="head">
</asp:Content>
<asp:Content ID="Content" runat="server" ContentPlaceHolderID="body">
    <form action="Default.aspx" id="form" runat="server" style="height:100%;width:100%" align="left" >
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <div  style="height:100%;width:100%" align="left" >
        </div>  
    </form>
</asp:Content>

Add a reference to Facebook.Web assembly:

 <%@ Register Assembly="Facebook.Web" Namespace="Facebook.Web" TagPrefix="cc1" %>
 Add a "MasterType" directive:
<%@ MasterType VirtualPath="~/Site.Master" %>
Remove the html code from Site.Master.aspx and add the this code:
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
    <link rel="stylesheet" type="text/css" href="style.css" />
    <asp:ContentPlaceHolder ID="head" runat="server"/>
</head>
<body style="position: absolute; left: 0px">
    <script src="http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php" type="text/javascript"></script>
     <asp:ContentPlaceHolder ID="body" runat="server">
    </asp:ContentPlaceHolder>
    <script type="text/javascript">
        FB_RequireFeatures(["CanvasUtil"], function() {
        FB.Facebook.init("0df0965cab2684a029288805523b2596", "channel/xd_receiver.htm");
        FB.CanvasClient.startTimerToSizeToContent();
        // For information about FB.CanvasClient.syncUrl(); see http://wiki.developers.facebook.com/index.php/IFrame_Url
        FB.CanvasClient.syncUrl();
    });
    </script>
 </body>
</html>
 

Change the Site.Master.cs file:

Set class SiteMaster to inherits Facebook.Web.CanvasIFrameMasterPage  instead System.Web.UI.MasterPage.

Remove the  Page_Load method and set a default ctor:

 public SiteMaster()
{
      RequireLogin = true;
}
Start your Facebook application.

Add a Silverlight application to our solution.

For this example is added version of Snake Snacks  game.

Add reference to Silverlight Application in ASP.Net project. 
Add <object></object> tag in Default.aspx for Silverlight application:
<%@ Page Title="Home Page" Language="C#" AutoEventWireup="true" MasterPageFile="~/Site.Master"   CodeBehind="Default.aspx.cs" Inherits="IFrameSnake.Default" %>
<%@ Register Assembly="Facebook.Web" Namespace="Facebook.Web" TagPrefix="cc1" %>
<%@ MasterType VirtualPath="~/Site.Master" %>
<asp:Content runat="server" ContentPlaceHolderID="head">
</asp:Content>
<asp:Content ID="Content" runat="server" ContentPlaceHolderID="body">
<form action="Default.aspx" id="form" runat="server" style="height:100%;width:100%" align="left" >
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <div  style="height:100%;width:100%" align="left" >
<object id="Silverlight1" type="application/x-silverlight-2"  data="data:application/x-silverlight," width="800" height="500"> 
                <param name="source" value="ClientBin/SnakeSnaks2.xap"/> 
                <param name="InitParams" value="<%=InitParams %>" />
            </object>
        </div>  
    </form>
</asp:Content>
 Add code in Page_Load method in Default.aspx.cs file:
    public partial class Default : System.Web.UI.Page
    {
        protected string InitParams { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
Facebook.Schema.user userinfo = Master.Api.Users.GetInfo(Master.Api.Users.GetLoggedInUser());
              string username = userinfo.first_name + " " + userinfo.last_name;
              InitParams = "username=" + username;
        }
    }
 Run the application
 

Part 3 of this article will demonstrate how to build Silverlight applications for Facebook using the Facebook API for Silverlight from the latest Facebook Developer Kit.