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

http://code.google.com/apis/youtube/developers_guide_protocol.html#Searching_for_Videos

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


  • first get a free Developer Key:  http://youtube.com/signup?next=/my_profile_dev.

  • now you need to construct uri 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"; //


  • 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
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];
this.DataList1.DataSource = dt;
DataList1.DataBind();
}



and here is your datalist code

<table>
<asp:Datalist id="DataList1" runat="server" 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>


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

No comments:

Post a Comment