.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Forum >> SharePoint >> Post New QuestionBookmark and Share Subscribe to Forum

Custom search web part in moss 2007

Posted By: Aruna     Posted Date: February 16, 2011    Points:2   Category :SharePoint
i have created a custom enterprise search web part and the results are displayed in the datagrid.
I need to export the data grid results in to an excel sheet....
Any code example would be helpful



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Data;
using System.Xml.Serialization;
using Microsoft.SharePoint.WebPartPages;
using Microsoft.Office.Server;
using Microsoft.Office.Server.Search.Query;
using System.Runtime.InteropServices;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Windows;
using Microsoft.SharePoint.WebControls;
namespace CustomSearchWebParts
{
// [DefaultProperty("Text")]
//[ToolboxData("<{0}:WebCustomControl1 runat=server></{0}:WebCustomControl1>")]
[XmlRoot(Namespace = "CustomSearchWebParts")]
public class AuthorSearchWebPart : WebPart
{
Button cmdSearch;
TextBox txtQueryText;
Label lblQueryResult;
DataGrid grdResults;
SPDataSource myDataSource = new SPDataSource();

protected override void CreateChildControls()
{
// Create and add the controls that compose the
// user interface of the Web Part.
Controls.Clear();

txtQueryText = new TextBox();
this.Controls.Add(txtQueryText);

cmdSearch = new Button();
cmdSearch.Text = "Start Search";
cmdSearch.Click += new EventHandler(cmdSearch_Click);
this.Controls.Add(cmdSearch);

lblQueryResult = new Label();
this.Controls.Add(lblQueryResult);
}

void cmdSearch_Click(object sender, EventArgs e)
{
if (txtQueryText.Text != string.Empty)
{
keywordQueryExecute(txtQueryText.Text);
}
else
{
lblQueryResult.Text = "You must enter a search word.";
}
}

private void keywordQueryExecute(string strQueryText)
{
KeywordQuery kRequest =
new KeywordQuery(ServerContext.Current);
string strQuery = "Keyword:" + strQueryText;
kRequest.QueryText = strQuery;

// Configure the query to return relevant results.
kRequest.ResultTypes |= ResultType.RelevantResults;

// Execute the query.
ResultTableCollection resultTbls = kRequest.Execute();

if ((int)ResultType.RelevantResults != 0)
{
ResultTable tblResult =
resultTbls[ResultType.RelevantResults];

if (tblResult.TotalRows == 0)
{
lblQueryResult.Text = "No Search Results Returned.";
}
else
{
ReadResultTable(tblResult);
}
}
}

void ReadResultTable(ResultTable rt)
{
// Create a DataSet and load the returned ResultTable into it.
DataTable relResultsTbl = new DataTable();
relResultsTbl.TableName = "Relevant Results";
DataSet ds = new DataSet("resultsset");
ds.Tables.Add(relResultsTbl);
ds.Load(rt, LoadOption.OverwriteChanges, relResultsTbl);

// Add the results to the DataGrid.
fillResultsGrid(ds);
}

private void fillResultsGrid(DataSet grdDs)
{
// Create an instance of the DataGrid and set its
// DataSource property to the supplied DataSet.
grdResults = new DataGrid();
grdResults.DataSource = grdDs;

// Set the display properties for the DataGrid.
grdResults.GridLines = GridLines.None;
grdResults.CellPadding = 4;
grdResults.Width = Unit.Percentage(100);
grdResults.ItemStyle.ForeColor = Color.Black;
grdResults.ItemStyle.BackColor = Color.AliceBlue;
grdResults.ItemStyle.Font.Size = FontUnit.Smaller;
grdResults.ItemStyle.Font.Name = "Tahoma";
grdResults.HeaderStyle.BackColor = Color.Navy;
grdResults.HeaderStyle.ForeColor = Color.White;
grdResults.HeaderStyle.Font.Bold = true;
grdResults.HeaderStyle.Font.Name = "Tahoma";
grdResults.HeaderStyle.Font.Size = FontUnit.Medium;

// Turn off AutoGenerate for the columns so that the DataGrid
// does not automatically bind to all of the columns in the
// search results set. Then create and configure the DataGrid
// to display only the desired columns.

grdResults.AutoGenerateColumns = false;
HyperLinkColumn colTitle = new HyperLinkColumn();
colTitle.DataTextField = "Title";
colTitle.HeaderText = "Title";
colTitle.DataNavigateUrlField = "Path";
grdResults.Columns.Add(colTitle);
BoundColumn colAuthor = new BoundColumn();
colAuthor.DataField = "Author";
colAuthor.HeaderText = "Author";
grdResults.Columns.Add(colAuthor);
BoundColumn PS = new BoundColumn();
PS.DataField = "Path";
PS.HeaderText = "Path";
grdResults.Columns.Add(PS);

// Bind the data to the DataGrid.
grdResults.DataBind();

//Add the DataGrid to the controls.
Controls.Add(grdResults);
}
}

}

Responses
Author: Sunil Yadav       [Moderator]      
Posted Date: February 16, 2011     Points: 5   

Hi,

Try out below code.

HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=Demo.xlsx");
HttpContext.Current.Response.Charset = "utf-8";
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
System.IO.StringWriter sw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw);
DataList1.RenderControl(htw);
Response.Write(sw.ToString());
HttpContext.Current.Response.Flush();


Let me know if you need any help.

Author: Aruna             
Posted Date: February 17, 2011     Points: 5   

Hi,

Thank you so much....

It worked. i can export to excel. one more help needed.
Any idea on how to set the scope (particular site/list) for this custom enterprise search web part?

Author: Sunil Yadav       [Moderator]      
Posted Date: February 17, 2011     Points: 5   


Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend