WSS[sharepoint]中如何建立一个搜索的功能的webpart[Search]

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中去这里就不介绍了。]
下面给完整代码

ExpandedBlockStart.gif ContractedBlock.gif /**/ /*written by King_astar on 2005.5.14
InBlock.gif * test search object in wws
ExpandedBlockEnd.gif
*/

None.gif
None.gif
using  System;
None.gif
using  System.ComponentModel;
None.gif
using  System.Web.UI;
None.gif
using  System.Web.UI.WebControls;
None.gif
using  System.Xml.Serialization;
None.gif
using  Microsoft.SharePoint;
None.gif
using  Microsoft.SharePoint.Utilities;
None.gif
using  Microsoft.SharePoint.WebPartPages;
None.gif
using  Microsoft.SharePoint.WebControls;
None.gif
None.gif
using  System.Data;
None.gif
None.gif
namespace  TestWebPartSearch
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// Description for WebPart1.
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    [DefaultProperty("Text"),
InBlock.gif        ToolboxData(
"<{0}:MySearchPart runat=server></{0}:MySearchPart>"),
InBlock.gif        XmlRoot(Namespace
="TestWebPartSearch")]
InBlock.gif    
public class MySearchPart : Microsoft.SharePoint.WebPartPages.WebPart
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private const string defaultText = "";
InBlock.gif        
private String _searchStr ="";
InBlock.gif        
private DataGrid mydatagrid ;
InBlock.gif        
private Button serarchBtn;
InBlock.gif        
private DropDownList selectdrp;
InBlock.gif        
private DropDownList fielddrp;
InBlock.gif
InBlock.gif        
private TextBox searchTxt;
InBlock.gif        
private String _searchColumn ="title";
InBlock.gif        
private SPSite _spsite;
InBlock.gif        
private SPWeb _spweb;
InBlock.gif
InBlock.gif
InBlock.gif        
private string text = defaultText;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
properties#region properties
InBlock.gif        [Browsable(
true),
InBlock.gif            Category(
"Miscellaneous"),
InBlock.gif            DefaultValue(defaultText),
InBlock.gif            WebPartStorage(Storage.Personal),
InBlock.gif            FriendlyName(
"List Item"),
InBlock.gif            Description(
"Text Property")]
InBlock.gif        
public string Text
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return text;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                text 
= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif    
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
constructor#region constructor
InBlock.gif        
public MySearchPart()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            _spsite 
=  new SPSite("http://172.16.1.212/");
InBlock.gif            
this._spweb =   _spsite.OpenWeb();
InBlock.gif            SPListCollection splists 
= _spweb.Lists;
InBlock.gif            serarchBtn 
= new Button();
InBlock.gif            selectdrp 
= new DropDownList();
InBlock.gif            searchTxt 
= new TextBox();
InBlock.gif            fielddrp 
= new DropDownList();
InBlock.gif            mydatagrid 
= new DataGrid();
InBlock.gif            
this.Controls.Add(new LiteralControl("Pls Select List:"));
InBlock.gif            
this.Controls.Add(selectdrp);
InBlock.gif            
this.Controls.Add(new LiteralControl("<br/>"));
InBlock.gif            
this.Controls.Add(fielddrp);
InBlock.gif            
this.Controls.Add(searchTxt);
InBlock.gif            
this.Controls.Add(serarchBtn);
InBlock.gif            
this.Controls.Add(mydatagrid);
InBlock.gif
InBlock.gif            serarchBtn.Text 
= " search Now";
InBlock.gif            serarchBtn.Click 
+=new EventHandler(serarchBtn_Click);
InBlock.gif            
for(int i=0;i<splists.Count;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                ListItem one 
= new ListItem();
InBlock.gif                one.Value 
= splists[i].Title;
InBlock.gif                one.Text  
=  splists[i].Title;
InBlock.gif                selectdrp.Items.Add(one);
InBlock.gif
ExpandedSubBlockEnd.gif            }

InBlock.gif            selectdrp.AutoPostBack 
= true;
InBlock.gif            selectdrp.SelectedIndexChanged 
+=new EventHandler(selectdrp_SelectedIndexChanged);
InBlock.gif            
InBlock.gif
InBlock.gif
InBlock.gif            
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
///    This method gets the custom tool parts for this Web Part by overriding the
InBlock.gif        
///    GetToolParts method of the WebPart base class. You must implement
InBlock.gif        
///    custom tool parts in a separate class that derives from 
InBlock.gif        
///    Microsoft.SharePoint.WebPartPages.ToolPart. 
InBlock.gif        
///    </summary>
ExpandedSubBlockEnd.gif        
///<returns>An array of references to ToolPart objects.</returns>

InBlock.gif//        public override ToolPart[] GetToolParts()
InBlock.gif
//        {
InBlock.gif
//            ToolPart[] toolparts = new ToolPart[2];
InBlock.gif
//            WebPartToolPart wptp = new WebPartToolPart();
InBlock.gif
//            CustomPropertyToolPart custom = new CustomPropertyToolPart();
InBlock.gif
//            toolparts[0] = wptp;
InBlock.gif
//            toolparts[1] = custom;
InBlock.gif
//            return toolparts;
InBlock.gif
//        }
InBlock.gif
        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Render this Web Part to the output parameter specified.
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="output"> The HTML writer to write out to </param>

InBlock.gif//        protected override void RenderWebPart(HtmlTextWriter output)
InBlock.gif
//        {
InBlock.gif
//            
InBlock.gif
//
InBlock.gif
//
InBlock.gif
//            output.Write(SPEncode.HtmlEncode(Text));
InBlock.gif
//        }
InBlock.gif

InBlock.gif        
protected override void CreateChildControls()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
base.CreateChildControls ();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void serarchBtn_Click(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
InBlock.gif            
InBlock.gif            String temp 
= fielddrp.SelectedValue;
InBlock.gif            
string typeStr = "";
InBlock.gif            
this._searchColumn = temp.Split(',')[0];
InBlock.gif            typeStr 
=  temp.Split(',')[1];
InBlock.gif            
this._searchStr = searchTxt.Text.Trim();
InBlock.gif            
//searchTxt.Text = temp;
InBlock.gif

InBlock.gif
InBlock.gif
InBlock.gif            SPList splist 
= _spweb.Lists[selectdrp.SelectedIndex];
InBlock.gif            SPQuery spquery 
= new SPQuery();
InBlock.gif            spquery.Query 
= "<Where><Eq><FieldRef Name=\""+_searchColumn+"\"/>"+ "<Value Type=\""+typeStr+"\">"+_searchStr+"</Value></Eq></Where>";
InBlock.gif            SPListItemCollection splistitems;
InBlock.gif            splistitems 
= splist.GetItems(spquery);
InBlock.gif            DataTable dt 
= new  DataTable();
InBlock.gif            dt.Columns.Add(
new DataColumn("ID",typeof(String)));
InBlock.gif            dt.Columns.Add(
new DataColumn("XML",typeof(String)));
InBlock.gif
InBlock.gif            
for(int i =0;i<splistitems.Count;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                DataRow row 
= dt.NewRow();
InBlock.gif                row[
"ID"]        = splistitems[i].ID;
InBlock.gif                row[
"XML"]  = this.Page.Server.HtmlEncode(splistitems[i].Xml);
InBlock.gif                dt.Rows.Add(row);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
InBlock.gif
InBlock.gif            
InBlock.gif            mydatagrid.DataSource 
= dt;
InBlock.gif            mydatagrid.DataBind();
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void selectdrp_SelectedIndexChanged(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            SPList splist 
= _spweb.Lists[selectdrp.SelectedIndex];
InBlock.gif            SPFieldCollection spfields 
= splist.Fields;
InBlock.gif            
for(int i=0;i<spfields.Count;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                ListItem one 
= new ListItem();
InBlock.gif                one.Value 
= spfields[i].InternalName+","+ spfields[i].TypeAsString;
InBlock.gif                one.Text  
= spfields[i].Title;
InBlock.gif                fielddrp.Items.Add(one);
InBlock.gif
ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

 



 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值