Adding Facebook Share Functionality To An ASP.NET Web Site With A Master Page

This article assumes the reader has experience developing web sites using ASP.NET, specifically using Master Pages. It also assumes a familiarity with Facebook. All sample code is in C#. After reading this article, you should be able to integrate Facebook Share with an ASP.NET web site, whether or not it contains a Master Page. I’ve chosen to illustrate this using a Master Page because it’s a little more involved, and once you can do it with a Master Page, you should be able to do it without one as well.

What is Facebook Share, and how does it work?

First, let’s review what Facebook Share actually is. Adding a Share widget to a web page allows a user to click on an icon which launches a Facebook Share dialog. If the user has a Facebook account, they can then share the web page on their Facebook “wall” with their friends on Facebook, adding comments if they so choose. Their friends will have the opportunity to share with their friends, and so on. In this way, a web page’s exposure can increase dramatically in a very short time.

According to the Facebook web site’s documentation there are five steps you need to take in order to integrate Share with a web page:

  • Step 1: Add a link to the Facebook Share application hosted by Facebook:


  • Step 2: Add a script tag that points to a Javascript component hosted by Facebook:


  • Step 3: Add a meta tag containing the title of the page:


  • Step 4: Add a meta tag containing a description of the page:


  • Step 5: Add a link tag pointing to an image to be used as a logo:


The Problem

All well and good, but we were talking about doing this while using a Master Page. See the problem yet? Well, if all the pages in our site use a common Master Page, which presumably contains the head tag, which in turn contains all the meta tags and link tags, how do we specify different tag values for different pages? Therein lies the problem. The solution lies in the code-behind file.

The Solution

I’ve chosen to place the code for Steps 1 and 2 in the actual web page as opposed to the Master Page, since that will allow us the flexibility to place the Share widget in varying locations on each page should we care to do so. However, I’m going to accomplish this by inserting an empty asp:Label component in the desired location in the aspx file, and assigning the appropriate value to it in the Page_Load event in the code-behind file. I’m going to use the same asp:Label component to hold the code for both Steps 1 and 2:

Next, we’ll instantiate a couple of HtmlMeta objects to handle the meta tags in Steps 3 and 4, also in the Page_Load event:

Finally, we’ll add the logo in Step 5 in the same Page_Load event using an HtmlLink object:

Creating these elements dynamically in the code-behind file allows us to add them to the head element of the Master Page without ever actually modifying the Master Page. You can use this same method for pages and/or sites that don’t make use of Master Pages.

Putting It All Together

The complete listing for the code-behind file is shown below. The only other change we made was to add that asp:Label control to the aspx file. Here’s the code behind file:

I hope that this article helps you get started integrating Facebook with ASP.NET web sites. For more information, check Facebook’s web site. The documentation there is not organized very well, but it’s there if you dig for it.

Dave Verschleiser
Murray Hill Technologies


DateComment
12/3/2009 1:02:35 PM Ed Wisniowski Nice work. Mind if I reverse engineer this for VB.NET and send it back to you for review.
1/5/2010 9:11:05 PM Eric CourvilleDave, Exceptional well written article. This is very helpful.
1/6/2010 7:25:03 AM Nazrul ChoudhuryGood article. Thanks Dave.
1/6/2010 5:31:38 PM awnithanks for the info, good work.
1/7/2010 10:24:55 AM ChalaloExcelent Post Dave! From Chile!
1/8/2010 6:59:04 AM Mohsinthx
1/8/2010 10:20:03 AM AntonellaFantastic post, thank you so much for sharing this.
1/8/2010 11:06:58 AM Anil JadhavGr88. Will use it.
1/8/2010 2:12:59 PM DinaishGreat article... keep it up.. Rgds Dinaish http://www.picmagic.co.in
1/8/2010 11:20:17 PM pascalit came at an opportune time! just when i was looking for it
1/9/2010 1:05:46 AM mohammed_awais_ali@yahoo.co.inthanks
1/10/2010 11:10:05 AM evethnx
1/11/2010 3:37:40 AM Ashraf MoradThanks it is useful
1/18/2010 9:54:35 PM krishnaThanq
1/21/2010 3:23:12 AM vishvas shendethank i am looking for this type exmple
2/8/2010 3:39:27 AM Deepakgrate work man very helpfull
2/19/2010 3:37:18 PM artzukAt localhost it didn't work until i noticed that facebook connects to the site and retriebes some info. Now everything is fine, thank you!

Enter your comments here:


Enter your name:   
 
 


===============================================================================================================