search
Japanese Chinese Nederlands Espanol Italiano Deutsch Francais Twitter Rss Feeds
MicrosoftArticlesForumsFAQs
C# .NET
VB.NET
Visual Studio .NET
ADO.NET
Xml / Xslt
VB 6.0
.NET CF
GDI+
LINQ
Deployment
Security
FoxPro
Silverlight / WPF
Entity Framework
RIA Services

Web ProgrammingArticlesForumsFAQs
JavaScript
ASP
ASP.NET
Web Services

Non-MicrosoftArticlesForumsFAQs
NHibernate
Perl
PHP
Ruby
Java
Linux / Unix
Apple
Open Source

DatabasesArticlesForumsFAQs
SQL Server
Access
Oracle
MySQL
Other Databases

OfficeArticlesForumsFAQs
Excel
Word
Powerpoint
Outlook
Publisher
Money

Operating SystemsArticlesForumsFAQs
Windows 7
Windows Server
Windows Vista
Windows XP
Windows Update
MAC
Linux / UNIX

Server PlatformsArticlesForumsFAQs
BizTalk
Site Server
Exhange Server
IIS

Graphic DesignArticlesForumsFAQs
Macromedia Flash
Adobe PhotoShop
Expression Blend
Expression Design
Expression Web

OtherArticlesForumsFAQs
Subversion / CVS
Ask Dr. Dotnetsky
Active Directory
Networking
Uninstall Virus
Job Openings
Product Reviews
Search Engines
Resumes

 

Add a YouTube Search Page to your ASP.NET Site


By Peter Bromberg
Printer Friendly Version
View My Articles
33 Views
    

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, and if you like, you can view a live copy of the page here.

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.
Please post questions at forums, not via email!

button
Article Discussion: Add a YouTube Search Page to your ASP.NET Site
Peter Bromberg posted at Saturday, August 04, 2007 12:14 PM
Original Article
 

YouTube Video Downloading asp.net
anarbek kalykov replied to Peter Bromberg at Wednesday, September 19, 2007 5:59 AM

Hi. Great article!!! Can you write something about YouTube Video Downloading???

I want to put something like "download video" on my site. Thanks.

www.uykucu.org

 

I don't think youtube offers downloads.
Peter Bromberg replied to anarbek kalykov at Wednesday, September 19, 2007 6:52 AM

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.

 

 

Thank you
anarbek kalykov replied to Peter Bromberg at Wednesday, September 19, 2007 7:01 AM
Thank you for your reply. I found nice script on the .net  as you mentioned :)
 

Preventing Fraud Clicks on Google Adsense
anarbek kalykov replied to Peter Bromberg at Friday, September 21, 2007 7:03 AM
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.
 

No it is not possible.
Peter Bromberg replied to anarbek kalykov at Friday, September 21, 2007 7:35 AM
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.