Integrating Google News Search in ASP.NET Project

Google News

Google News is a free news aggregator provided and operated by Google, selecting up-to-date news from thousands of publications.The service covers news articles appearing within the past 30 days on various news websites. In total, Google News aggregates content from more than 25,000 publishers. For the English language, it covers about 4,500 sites for other languages. Its front page provides roughly the first 200 characters of the article and a link to its larger content. Websites may or may not require a subscription; sites requiring subscriptions are noted in the article description.

In this article I would like to explain how you can integrate this service in your asp.net web application. For this we require reference to a dll called(GoogleSearchApI.dll).

You can download this dll from the following link given below.

For using this functionality in your website first create a new project and add a new web form into this.

Download the dll and extract this from the folder.

Right Click and Add new Referance.Browse and add the reference.

 
Browse the Dll and add to the reference.

 

After adding it will appear within your project like this.

 
 

Now every thing is set; now you need to call the api and show the new one from your page.

Now design your page like this.

 

Here I have attached the html code for this.
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="News.aspx.cs" Inherits="News" %>  
  2.   
  3. <!DOCTYPE html>  
  4.   
  5. <html xmlns="http://www.w3.org/1999/xhtml">  
  6. <head runat="server">  
  7.     <title></title>  
  8. </head>  
  9. <body>  
  10.     <form id="form1" runat="server">  
  11.         <asp:TextBox ID="txt_search" runat="server" Width="500px" Height="30px" ></asp:TextBox>  
  12.         <asp:Button runat="server" Text="SEARCH" ID="btn_btn" Height="40px" OnClick="btn_btn_Click" CommandName="Search" />  
  13.      
  14.         <br />  
  15.          
  16.         <asp:DataList ID="DataList1" runat="server" BackColor="#CCCCCC" BorderColor="#999999" BorderStyle="Solid" BorderWidth="3px" CellPadding="4" GridLines="Both" CellSpacing="2" ForeColor="Black" OnItemDataBound="DataList1_ItemDataBound">  
  17.             <FooterStyle BackColor="#CCCCCC" />  
  18.             <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />  
  19.             <ItemStyle BackColor="White" />  
  20.             <SelectedItemStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />  
  21.             <HeaderTemplate>  
  22.                    <div>  
  23.                        Today News  
  24.       
  25.                   </div>  
  26.                   
  27.             </HeaderTemplate>  
  28.             <ItemTemplate>  
  29.                 <div>  
  30.                       <asp:Image ID="imgEmp" runat="server" Width="100px" Height="120px" ImageUrl='<%# Bind("Image") %>' style="padding-left:40px"/><br />  
  31.                      
  32.                 </div>  
  33.                 <div>  
  34.                     <asp:Label runat="server" ID="lbl_content" Text='<%#Eval("Content") %>' Font-Bold="true">  

Now the design part is over. Now we have to start coading for getting the news. Before writting any code regarding this you should use the following Namespace. 

  1. using Google.API.Search;  

Here is the code that will give all news deatils.

  1. GnewsSearchClient client = new GnewsSearchClient("myclient");    
  2.   
  3.     IList<INewsResult> results=client.Search("Any Topics News", 10);    
This will brings you all the news .Here I have set the limit as 10 mence for any topic; 10 pieces of news will come at one time.

Now here I have shown the complete program to show the news creating a datatable and binding this into a datalist.
  1. protected void Page_Load(object sender, EventArgs e)  
  2.     {  
  3.         if(!IsPostBack)  
  4.         {  
  5.             GnewsSearchClient client = new GnewsSearchClient("myclient");  
  6.             IList<INewsResult> results;  
  7.             results = client.Search("India News", 10);  
  8.               
  9.   
  10.   
  11.             DataSet ds = new DataSet();  
  12.             DataTable dt = new DataTable();  
  13.   
  14.             dt.Columns.Add(new DataColumn("Content"typeof(string)));  
  15.             dt.Columns.Add(new DataColumn("Url"typeof(string)));  
  16.             dt.Columns.Add(new DataColumn("Image"typeof(string)));  
  17.             dt.Columns.Add(new DataColumn("Link"typeof(string)));  
  18.             dt.Columns.Add(new DataColumn("Publisher"typeof(string)));  
  19.             dt.Columns.Add(new DataColumn("Date"typeof(string)));  
  20.   
  21.   
  22.   
  23.   
  24.             foreach (var news in results)  
  25.             {  
  26.   
  27.                 DataRow dr = dt.NewRow();  
  28.   
  29.                 dr["Content"] = news.Content;  
  30.   
  31.                 dr["Image"] = news.Image;  
  32.                 dr["Link"] = news.Url;  
  33.                 
  34.                 dt.Rows.Add(dr);  
  35.                 DataList1.DataSource = dt;  
  36.                 DataList1.DataBind();  
  37.   
  38.   
  39.             }  
  40.   
  41.          
  42.         }  
  43.   
  44.     }  

So here when the page loads for first time it will show Indi's news as follows.

 

Now here is the complete code to search any type of news.

  1. using Google.API.Search;  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.Linq;  
  5. using System.Web;  
  6. using System.Web.UI;  
  7. using System.Web.UI.WebControls;  
  8. using System.Data;  
  9.   
  10.   
  11. public partial class News : System.Web.UI.Page  
  12. {  
  13.      
  14.     protected void Page_Load(object sender, EventArgs e)  
  15.     {  
  16.         GnewsSearchClient client = new GnewsSearchClient("myclient");  
  17.         IList<INewsResult> results;  
  18.         if(!IsPostBack)  
  19.         {  
  20.              
  21.             results = client.Search("India News", 10);  
  22.               
  23.   
  24.   
  25.             DataSet ds = new DataSet();  
  26.             DataTable dt = new DataTable();  
  27.   
  28.             dt.Columns.Add(new DataColumn("Content"typeof(string)));  
  29.             dt.Columns.Add(new DataColumn("Url"typeof(string)));  
  30.             dt.Columns.Add(new DataColumn("Image"typeof(string)));  
  31.             dt.Columns.Add(new DataColumn("Link"typeof(string)));  
  32.             dt.Columns.Add(new DataColumn("Publisher"typeof(string)));  
  33.             dt.Columns.Add(new DataColumn("Date"typeof(string)));  
  34.   
  35.   
  36.   
  37.   
  38.             foreach (var news in results)  
  39.             {  
  40.   
  41.                 DataRow dr = dt.NewRow();  
  42.   
  43.                 dr["Content"] = news.Content;  
  44.   
  45.                 dr["Image"] = news.Image;  
  46.                 dr["Link"] = news.Url;  
  47.                 
  48.                 dt.Rows.Add(dr);  
  49.                 DataList1.DataSource = dt;  
  50.                 DataList1.DataBind();  
  51.   
  52.   
  53.             }  
  54.   
  55.          
  56.         }  
  57.   
  58.     }  
  59.     public  void searchNews(string prefix)  
  60.     {  
  61.   
  62.         results = client.Search(prefix, 10);  
  63.   
  64.   
  65.         DataSet ds = new DataSet();  
  66.         DataTable dt = new DataTable();  
  67.         
  68.         dt.Columns.Add(new DataColumn("Content"typeof(string)));  
  69.         dt.Columns.Add(new DataColumn("Url"typeof(string)));  
  70.         dt.Columns.Add(new DataColumn("Image"typeof(string)));  
  71.         dt.Columns.Add(new DataColumn("Link"typeof(string)));  
  72.           
  73.   
  74.   
  75.           
  76.         foreach (var news in results)  
  77.         {  
  78.   
  79.             DataRow dr = dt.NewRow();  
  80.               
  81.             dr["Content"] = news.Content;  
  82.   
  83.             dr["Image"] = news.Image;  
  84.             dr["Link"] = news.Url;  
  85.             
  86.             dt.Rows.Add(dr);  
  87.             DataList1.DataSource = dt;  
  88.             DataList1.DataBind();  
  89.   
  90.   
  91.         }  
  92.   
  93.          
  94.   
  95.     }  
  96.     protected void btn_btn_Click(object sender, EventArgs e)  
  97.     {  
  98.   
  99.         searchNews(txt_search.Text);  
  100.            
  101.        
  102.          
  103.     }  
  104.       
  105. }  

Now we can run this application and search any kind of news inside our website as follows.


Thus in this way we can inplement google news search functionality in our website.


Similar Articles