with Windows sharepoint service, how to build a webpart to support search one list of a web site.
这个webpart的使用必须让MSSQL支持全文索引才行。
在sql客户端运行
sp_fulltext_database 'enable'
使用时要小心。如果已经存在全文目录,那么该过程将除去所有的全文目录,重新创建系统表中指明的任何全文索引,并且将数据库标记为已全文启用。
这个webpart例子是显示一个站点所有的list,并把每个list的field显示出来,同时根据输入查询的字符串来查询这个list的记录。
[如何创建webpart和如何加入webpart到wss中去这里就不介绍了。]
下面给完整代码
/*written by King_astar on 2005.5.14
* test search object in wws
*/
using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Serialization;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.WebPartPages;
using Microsoft.SharePoint.WebControls;
using System.Data;
namespace TestWebPartSearch
{
/// <summary>
/// Description for WebPart1.
/// </summary>
[DefaultProperty("Text"),
ToolboxData("<{0}:MySearchPart runat=server></{0}:MySearchPart>"),
XmlRoot(Namespace="TestWebPartSearch")]
public class MySearchPart : Microsoft.SharePoint.WebPartPages.WebPart
{
private const string defaultText = "";
private String _searchStr ="";
private DataGrid mydatagrid ;
private Button serarchBtn;
private DropDownList selectdrp;
private DropDownList fielddrp;
private TextBox searchTxt;
private String _searchColumn ="title";
private SPSite _spsite;
private SPWeb _spweb;
private string text = defaultText;
#region properties
[Browsable(true),
Category("Miscellaneous"),
DefaultValue(defaultText),
WebPartStorage(Storage.Personal),
FriendlyName("List Item"),
Description("Text Property")]
public string Text
{
get
{
return text;
}
set
{
text = value;
}
}
#endregion
#region constructor
public MySearchPart()
{
_spsite = new SPSite("http://172.16.1.212/");
this._spweb = _spsite.OpenWeb();
SPListCollection splists = _spweb.Lists;
serarchBtn = new Button();
selectdrp = new DropDownList();
searchTxt = new TextBox();
fielddrp = new DropDownList();
mydatagrid = new DataGrid();
this.Controls.Add(new LiteralControl("Pls Select List:"));
this.Controls.Add(selectdrp);
this.Controls.Add(new LiteralControl("<br/>"));
this.Controls.Add(fielddrp);
this.Controls.Add(searchTxt);
this.Controls.Add(serarchBtn);
this.Controls.Add(mydatagrid);
serarchBtn.Text = " search Now";
serarchBtn.Click +=new EventHandler(serarchBtn_Click);
for(int i=0;i<splists.Count;i++)
{
ListItem one = new ListItem();
one.Value = splists[i].Title;
one.Text = splists[i].Title;
selectdrp.Items.Add(one);
}
selectdrp.AutoPostBack = true;
selectdrp.SelectedIndexChanged +=new EventHandler(selectdrp_SelectedIndexChanged);
}
#endregion
/// <summary>
/// This method gets the custom tool parts for this Web Part by overriding the
/// GetToolParts method of the WebPart base class. You must implement
/// custom tool parts in a separate class that derives from
/// Microsoft.SharePoint.WebPartPages.ToolPart.
/// </summary>
///<returns>An array of references to ToolPart objects.</returns>
// public override ToolPart[] GetToolParts()
// {
// ToolPart[] toolparts = new ToolPart[2];
// WebPartToolPart wptp = new WebPartToolPart();
// CustomPropertyToolPart custom = new CustomPropertyToolPart();
// toolparts[0] = wptp;
// toolparts[1] = custom;
// return toolparts;
// }
/// <summary>
/// Render this Web Part to the output parameter specified.
/// </summary>
/// <param name="output"> The HTML writer to write out to </param>
// protected override void RenderWebPart(HtmlTextWriter output)
// {
//
//
//
// output.Write(SPEncode.HtmlEncode(Text));
// }
protected override void CreateChildControls()
{
base.CreateChildControls ();
}
private void serarchBtn_Click(object sender, EventArgs e)
{
String temp = fielddrp.SelectedValue;
string typeStr = "";
this._searchColumn = temp.Split(',')[0];
typeStr = temp.Split(',')[1];
this._searchStr = searchTxt.Text.Trim();
//searchTxt.Text = temp;
SPList splist = _spweb.Lists[selectdrp.SelectedIndex];
SPQuery spquery = new SPQuery();
spquery.Query = "<Where><Eq><FieldRef Name=\""+_searchColumn+"\"/>"+ "<Value Type=\""+typeStr+"\">"+_searchStr+"</Value></Eq></Where>";
SPListItemCollection splistitems;
splistitems = splist.GetItems(spquery);
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("ID",typeof(String)));
dt.Columns.Add(new DataColumn("XML",typeof(String)));
for(int i =0;i<splistitems.Count;i++)
{
DataRow row = dt.NewRow();
row["ID"] = splistitems[i].ID;
row["XML"] = this.Page.Server.HtmlEncode(splistitems[i].Xml);
dt.Rows.Add(row);
}
mydatagrid.DataSource = dt;
mydatagrid.DataBind();
}
private void selectdrp_SelectedIndexChanged(object sender, EventArgs e)
{
SPList splist = _spweb.Lists[selectdrp.SelectedIndex];
SPFieldCollection spfields = splist.Fields;
for(int i=0;i<spfields.Count;i++)
{
ListItem one = new ListItem();
one.Value = spfields[i].InternalName+","+ spfields[i].TypeAsString;
one.Text = spfields[i].Title;
fielddrp.Items.Add(one);
}
}
}
}
* test search object in wws
*/
using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Serialization;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.WebPartPages;
using Microsoft.SharePoint.WebControls;
using System.Data;
namespace TestWebPartSearch
{
/// <summary>
/// Description for WebPart1.
/// </summary>
[DefaultProperty("Text"),
ToolboxData("<{0}:MySearchPart runat=server></{0}:MySearchPart>"),
XmlRoot(Namespace="TestWebPartSearch")]
public class MySearchPart : Microsoft.SharePoint.WebPartPages.WebPart
{
private const string defaultText = "";
private String _searchStr ="";
private DataGrid mydatagrid ;
private Button serarchBtn;
private DropDownList selectdrp;
private DropDownList fielddrp;
private TextBox searchTxt;
private String _searchColumn ="title";
private SPSite _spsite;
private SPWeb _spweb;
private string text = defaultText;
#region properties
[Browsable(true),
Category("Miscellaneous"),
DefaultValue(defaultText),
WebPartStorage(Storage.Personal),
FriendlyName("List Item"),
Description("Text Property")]
public string Text
{
get
{
return text;
}
set
{
text = value;
}
}
#endregion
#region constructor
public MySearchPart()
{
_spsite = new SPSite("http://172.16.1.212/");
this._spweb = _spsite.OpenWeb();
SPListCollection splists = _spweb.Lists;
serarchBtn = new Button();
selectdrp = new DropDownList();
searchTxt = new TextBox();
fielddrp = new DropDownList();
mydatagrid = new DataGrid();
this.Controls.Add(new LiteralControl("Pls Select List:"));
this.Controls.Add(selectdrp);
this.Controls.Add(new LiteralControl("<br/>"));
this.Controls.Add(fielddrp);
this.Controls.Add(searchTxt);
this.Controls.Add(serarchBtn);
this.Controls.Add(mydatagrid);
serarchBtn.Text = " search Now";
serarchBtn.Click +=new EventHandler(serarchBtn_Click);
for(int i=0;i<splists.Count;i++)
{
ListItem one = new ListItem();
one.Value = splists[i].Title;
one.Text = splists[i].Title;
selectdrp.Items.Add(one);
}
selectdrp.AutoPostBack = true;
selectdrp.SelectedIndexChanged +=new EventHandler(selectdrp_SelectedIndexChanged);
}
#endregion
/// <summary>
/// This method gets the custom tool parts for this Web Part by overriding the
/// GetToolParts method of the WebPart base class. You must implement
/// custom tool parts in a separate class that derives from
/// Microsoft.SharePoint.WebPartPages.ToolPart.
/// </summary>
///<returns>An array of references to ToolPart objects.</returns>
// public override ToolPart[] GetToolParts()
// {
// ToolPart[] toolparts = new ToolPart[2];
// WebPartToolPart wptp = new WebPartToolPart();
// CustomPropertyToolPart custom = new CustomPropertyToolPart();
// toolparts[0] = wptp;
// toolparts[1] = custom;
// return toolparts;
// }
/// <summary>
/// Render this Web Part to the output parameter specified.
/// </summary>
/// <param name="output"> The HTML writer to write out to </param>
// protected override void RenderWebPart(HtmlTextWriter output)
// {
//
//
//
// output.Write(SPEncode.HtmlEncode(Text));
// }
protected override void CreateChildControls()
{
base.CreateChildControls ();
}
private void serarchBtn_Click(object sender, EventArgs e)
{
String temp = fielddrp.SelectedValue;
string typeStr = "";
this._searchColumn = temp.Split(',')[0];
typeStr = temp.Split(',')[1];
this._searchStr = searchTxt.Text.Trim();
//searchTxt.Text = temp;
SPList splist = _spweb.Lists[selectdrp.SelectedIndex];
SPQuery spquery = new SPQuery();
spquery.Query = "<Where><Eq><FieldRef Name=\""+_searchColumn+"\"/>"+ "<Value Type=\""+typeStr+"\">"+_searchStr+"</Value></Eq></Where>";
SPListItemCollection splistitems;
splistitems = splist.GetItems(spquery);
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("ID",typeof(String)));
dt.Columns.Add(new DataColumn("XML",typeof(String)));
for(int i =0;i<splistitems.Count;i++)
{
DataRow row = dt.NewRow();
row["ID"] = splistitems[i].ID;
row["XML"] = this.Page.Server.HtmlEncode(splistitems[i].Xml);
dt.Rows.Add(row);
}
mydatagrid.DataSource = dt;
mydatagrid.DataBind();
}
private void selectdrp_SelectedIndexChanged(object sender, EventArgs e)
{
SPList splist = _spweb.Lists[selectdrp.SelectedIndex];
SPFieldCollection spfields = splist.Fields;
for(int i=0;i<spfields.Count;i++)
{
ListItem one = new ListItem();
one.Value = spfields[i].InternalName+","+ spfields[i].TypeAsString;
one.Text = spfields[i].Title;
fielddrp.Items.Add(one);
}
}
}
}