Add a YouTube Search Page to your ASP.NET Site

By Peter Bromberg

Shows how to easily create a YouTube Search page using the YouTube RESTful API and a simple DataList control.

YouTube is a popular video sharing website where users can upload, view and share video clips. YouTube was created in mid February 2005 by three former PayPal employees. The San Bruno-based service uses Adobe Flash technology to display a wide variety of video content, including movie clips, TV clips and music videos, as well as amateur content such as videoblogging and short original videos. In October 2006, Google Inc. announced that it had reached a deal to acquire the company for US$1.65 billion in Google stock. The deal closed on November 13, 2006.

Since YouTube has become so popular, it occured to me that I might want to add a YouTube Search Page to my "playground site", IttyUrl.net. It turned out that with the REST - based YouTube developer API, it was a "piece of cake".

Here is how you can add a very simple yet effective YouTube search page to your ASP.NET  web site:

1) First, you need to get a free Developer Key:  http://youtube.com/signup?next=/my_profile_dev. This is required to use the API. When you've got your developerKey,  put it in the developerKey appSettings element in the web.config.
2) Next you will construct a REST call to the youtube.videos.list_by_tag method, passing the tag (search phrase), your developer key, and paging information.
   A fully constructed dynamic REST uri would be constructed like this:

    string uri = "http://www.youtube.com/api2_rest?";
            uri += "method=youtube.videos.list_by_tag";
             uri += "&dev_id=" + developerKey;
            uri += "&tag=" + txtSearch.Text;
            uri += "&page=1&per_page=50"; // you can add custom paging if desired


3) Since the results are returned as an XML document, there is no need to get involved in a lot of XPATH - simply load the XML into a DataSet using its ReadXml method.
 
4) The table that holds the information you need is DataSet.Tables[2] - the third table in the resulting dataset.
5) I inserted some rows with my own Google Adsense ad to show how easy it is to add advertising to the resultset.
6) Bind the DataTable to a templated DataList - you are done!

Here is the complete codebehind for the search page:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace YouTubeSearch
{
    public partial class YouTube : System.Web.UI.Page
    {
         protected void Page_Load(object sender, EventArgs e)
        {
        }

         protected void Button1_Click(object sender, EventArgs e)
        {
            string developerKey = ConfigurationManager.AppSettings["developerKey"];
            if (developerKey.Length == 0)
             {
                 Response.Write("You need to get a <a href=http://youtube.com/signup?next=/my_profile_dev> YouTube developer key</a> first!");
                 return;
            }
            // Call the YouTube api to list all videos for a tag
            string uri = "http://www.youtube.com/api2_rest?";
            uri += "method=youtube.videos.list_by_tag";
             uri += "&dev_id=" + developerKey;
            uri += "&tag=" + txtSearch.Text;
            uri += "&page=1&per_page=50"; // you can add custom paging if desired
            DataSet ds = new DataSet();
            ds.ReadXml(uri);
            DataTable dt = ds.Tables[2];
             PlaceAds(dt);
             this.DataList1.DataSource = dt;
            DataList1.DataBind();
        }

      
        protected void PlaceAds(DataTable tbl)
        {
            string adCode = ConfigurationManager.AppSettings["adCode"];
            adCode = Server.HtmlDecode(adCode);

             int adpos = tbl.Rows.Count / 3;
            DataRow rowAd = tbl.NewRow();
             rowAd["description"] = adCode;
             rowAd["thumbnail_url"] = "images/sponsor.gif";
            tbl.Rows.InsertAt(rowAd, 1);
             if (tbl.Rows.Count > 10)
            {
                rowAd = tbl.NewRow();
                 rowAd["description"] = adCode;
                 rowAd["thumbnail_url"] = "images/sponsor.gif";
                tbl.Rows.InsertAt(rowAd, adpos);
                rowAd = tbl.NewRow();
                 rowAd["description"] = adCode;
                 rowAd["thumbnail_url"] = "images/sponsor.gif";
                tbl.Rows.InsertAt(rowAd, adpos * 2);
             }
        }
     }
}

Here is what the markup for the DataList looks like:

<table cellspacing="2" cellpadding="2" border="0">      
<asp:Datalist id="DataList1" runat="server" Font-Names="Verdana" CellPadding="2" CellSpacing ="2" BorderStyle="None">
<FooterStyle BackColor="#CCCCCC" ForeColor="Black"></FooterStyle>
<ItemTemplate>
<tr  ><td colspan="2" align=center> <b><%# DataBinder.Eval(Container, "DataItem.title") %>   </b>  </td></tr>
  <tr>
<td style="width:100px;Height:20px;">
    <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("url") %>' >
    <asp:Image ID="Img" ImageUrl='<%#Eval("thumbnail_url") %>' runat="Server" />
    </asp:HyperLink>
    </td>
<td style="width:450px;Height:20px;"><%# DataBinder.Eval(Container, "DataItem.description") %></td>
        </tr>        
</ItemTemplate>
</asp:Datalist>      
</table>


You can download the complete Visual Studio 2005 Solution.

Popularity  (2891 Views)
Picture
Biography - Peter Bromberg
Peter Bromberg is a C# MVP, MCP, and .NET expert who has worked in banking, financial and telephony for over 20 years. Pete focuses exclusively on the .NET Platform, and currently develops SOA and other .NET applications for a Fortune 500 clientele. Peter enjoys producing digital photo collage with Maya,playing jazz flute, the beach, and fine wines. You can view Peter's UnBlog and IttyUrl sites. Follow Microsoft MVP
Create New Account
Article Discussion: Add a YouTube Search Page to your ASP.NET Site
Peter Bromberg posted at Saturday, August 04, 2007 12:14 PM
reply
I don't think youtube offers downloads.
Peter Bromberg replied to anarbek kalykov at Saturday, December 31, 2011 2:39 PM

you can only link (embed html) to the video. I'm sure there are plenty of "hack" scripts that allow you to download the FLV video file, but that's not our domain.

 

reply
Thank you
anarbek kalykov replied to Peter Bromberg at Saturday, December 31, 2011 2:39 PM
Thank you for your reply. I found nice script on the .net  as you mentioned :)
reply
Preventing Fraud Clicks on Google Adsense
anarbek kalykov replied to Peter Bromberg at Saturday, December 31, 2011 2:39 PM
Hi Peter. I ran into another problem while developing my site. Is it possible to prevent fraud clicks on Google AdSense? I did'n find any appropriate controls on the net. Do you have any idea on this??? Thank you.
reply
No it is not possible.
Peter Bromberg replied to anarbek kalykov at Saturday, December 31, 2011 2:39 PM
Why would anyone want to generate fraudulent clicks on adsense ads, except the owner of the site? And in that case google would find out right away and turn you off.
reply