Wednesday, October 7, 2009

youtube search api using C#

hi , i have allready specify how you can use youtube api in your .net application, here is my next post on same topic , you can add Youtube Search widget on your web site using API SEARCH method ,XmlDataSource control can be used in ASP.Net to bind the Youtube API RSS response to display the Youtube videos.

if you are using .Net then Repeater Control is one of the best option to display result . at first you need to create api method url to retrive the result from youtube in terms of RSS Feed. you can also Use of XmlNamespaceManager to read that Rss feed having media namespace that provides the syndication content about Youtube videos

- Google Code for Youtube API and Tools

Steps to add simple YouTube search page to your ASP.NET web site

  • first get a free Developer Key:

  • now you need to construct uri like this

    string uri = "";
    uri += "method=youtube.videos.list_by_tag";
    uri += "&dev_id=" + developerKey;
    uri += "&tag=" + txtSearch.Text;
    uri += "&page=1&per_page=50"; //

  • now you can get the returned result in an XML document,now load the XML into a DataSet using its ReadXml method.

now create one page and in that create one text box and one submit button
put this code on codebehind

protected void Button1_Click(object sender, EventArgs e)
string developerKey = "" // put your key here;
if (developerKey.Length == 0)
Response.Write("You need to get a YouTube

developer key
// Call the YouTube api to list all videos for a tag
string uri = "";
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();
DataTable dt = ds.Tables[2];
this.DataList1.DataSource = dt;

and here is your datalist code

<asp:Datalist id="DataList1" runat="server" CellPadding="2" CellSpacing ="2" BorderStyle="None">
<FooterStyle BackColor="#CCCCCC" ForeColor="Black"></FooterStyle>
<tr > <td colspan="2" align=center> <b> <%# DataBinder.Eval(Container, "DataItem.title") %> </b> </td> </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" />
<td style="width:450px;Height:20px;"><%# DataBinder.Eval(Container, "DataItem.description") %></td>

view demo
that's it , happy youtubing...

No comments:

Post a Comment