为List配置一个搜索按钮

查询前的List首页,只有一个cusotmer
customer下面有个Project文件夹和Cusotmer Complaint文件
其中Customer Complaint.txt文件是有多个属性的
现在开始针对文档库中所有的文件 和文件夹进行模糊查询
ExpandedBlockStart.gif 前段代码
<% @ Control Language = " C# "  AutoEventWireup = " true "  CodeBehind = " SearchList.ascx.cs "  Inherits = " MySharePointDev.Public.SearchList "   %>
< table >
    
< tr >
        
< td >
            
< asp:Label  ID ="labKeyWord"  runat ="server"  Text ="KeyWord:"  Font-Bold ="True"   ></ asp:Label >
            
< asp:TextBox  ID ="tbKeyWord"  runat ="server"   ></ asp:TextBox >
            
< asp:Button  ID ="btnSearch"  runat ="server"  onclick ="btnSearch_Click"  
                Text
="Search"   />
        
</ td >
    
</ tr >
</ table >

 

ExpandedBlockStart.gif 后端代码
using  System;
using  System.Collections.Generic;
using  System.Linq;
using  System.Web;
using  System.Web.UI;
using  System.Web.UI.WebControls;
using  Microsoft.SharePoint;
using  Microsoft.SharePoint.WebPartPages;
using  System.Xml;
using  System.Web.UI.WebControls.WebParts;

namespace  MySharePointDev.Public
{
    
public   partial   class  SearchList : BaseSPWebPart
    {
        
private   string  _QueryFields  =   " Title; " ;
        [Personalizable(PersonalizationScope.Shared)]
        [WebBrowsable]
        [WebDisplayName(
" 查询的字段显示名,以分号分隔开 " )]
        
public   string  QueryFields
        {
            
get  
            {
                
if  (_QueryFields.EndsWith( " ; " )) // 防止以;结束
                {
                    
return  _QueryFields.Substring( 0 , _QueryFields.Length  -   2 );
                }
                
else
                {
                    
return  _QueryFields;
                }
            }
            
set  { _QueryFields  =  value; }
        }


        
private  SPList List
        {
            
get
            {
                
return  SPContext.Current.List;
            }
        }

        
protected   void  Page_Load( object  sender, EventArgs e)
        {
            
// this.ExportMode = WebPartExportMode.All;
            tbKeyWord.ToolTip  =   " Please input:  "   +  QueryFields;
        }

        
///   <summary>
        
///  search
        
///   </summary>
        
///   <param name="sender"></param>
        
///   <param name="e"></param>

        
protected   void  btnSearch_Click( object  sender, EventArgs e)
        {
            
if  ( this .tbKeyWord.Text  !=   "" )
            {
                
string [] fields  =  QueryFields.Split( ' ; ' );
                
string  cmal  =   "" ;
                
foreach  ( string  field  in  fields)
                {
                    
if  (cmal  ==   "" // 第一个参数
                    {
                        cmal 
=   string .Format( @" <Contains>
                                                              <FieldRef Name='{0}' />
                                                              <Value Type='{1}'>{2}</Value>
                                                           </Contains>
                                                           
" , List.Fields[field].InternalName, List.Fields[field].Type.ToString(),  this .tbKeyWord.Text.ToString());
                    }
                    
else
                    {
                        cmal 
=   " <Or> "   +  cmal  +   string .Format( @" <Contains>
                                                              <FieldRef Name='{0}' />
                                                              <Value Type='{1}'>{2}</Value>
                                                           </Contains></Or>
                                                           
" , List.Fields[field].InternalName, List.Fields[field].Type.ToString(),  this .tbKeyWord.Text.ToString());
                    }
                    
                }
                cmal 
=   " <Where> "   +  cmal  +   " </Where> " ;
                
this .SetCurrentListViewSchemaQuery(cmal);
                
            }

        }

        
///   <summary>
        
///  设置当前list的视图 schema query 
        
///   </summary>
        
///   <param name="cmal"></param>
         private   void  SetCurrentListViewSchemaQuery( string  cmal)
        {
            
if  ( ! string .IsNullOrEmpty(cmal))
            {
                
string  str  =   " { "   +   this .List.ID.ToString()  +   " } " ;

                ControlCollection cc 
=   this .Page.Controls;
                GetControls(
ref   cc, str, cmal);


            }
        }
        
///   <summary>
        
///  遍历所有页面中所有控件
        
///   </summary>
        
///   <param name="cs"> 页面控件集 </param>
        
///   <param name="str"></param>
        
///   <param name="cmal"></param>
         private   void  GetControls( ref  ControlCollection cs,  string  str,  string  cmal)
        {
            
foreach  (Control webPart  in  cs)
            {
                
if  (webPart.HasControls())
                {
                    ControlCollection cc 
=  webPart.Controls;
                    GetControls(
ref   cc, str, cmal);
                }

                
if  (webPart  is  ListViewWebPart)
                {
                    ListViewWebPart listViewWebPart 
=  (ListViewWebPart)webPart;
                    
if  ( string .Compare(listViewWebPart.ListName, str,  true !=   0 )
                    {
                        
continue ;
                    }

                    
if  ( string .IsNullOrEmpty(cmal))
                    {
                        listViewWebPart.ListViewXml 
=   this .List.Views[ new  Guid(listViewWebPart.ViewGuid)].HtmlSchemaXml;

                    }
                    
else
                    {
                        
// 申明个新的XmlDocument,将listViewWebPart的ListViewXml装载进去
                        XmlDocument xmlDocument  =   new  XmlDocument();
                        xmlDocument.LoadXml(listViewWebPart.ListViewXml);

                        
// 用cmal替换掉原有的查询语句
                         this .ChangeSchemaXmlCaml(xmlDocument, cmal);
                        listViewWebPart.ListViewXml 
=  xmlDocument.InnerXml;
                    }
                }
            }
        }

        
///   <summary>
        
///  move where 去除外部的Where和/Where
        
///   </summary>
        
///   <param name="q"></param>
        
///   <returns></returns>
         private   string  GetInnerQuery( string  q)
        {
            XmlDocument docuemnt 
=   new  XmlDocument();
            docuemnt.LoadXml(q);
            
return  docuemnt.DocumentElement.InnerXml;
        }
        
///   <summary>
        
///  change schema xml query 
        
///   </summary>
        
///   <param name="xmlDocument"></param>
        
///   <param name="query"></param>
         private   void  ChangeSchemaXmlCaml(XmlDocument xmlDocument,  string  query)
        {
            
if  ( ! string .IsNullOrEmpty(query))
            {
                
string  innerXml  =   this .GetInnerQuery(query);
                
if  (innerXml  !=   "" )
                {
                    
// 获得Query部分的XmlNode
                    XmlNode node  =  xmlDocument.DocumentElement.SelectSingleNode( " Query " );
                    XmlNode oldChild 
=  node.SelectSingleNode( " Where " );

                    
// 如果有Where,去除它
                     if  (oldChild  !=   null )
                    {
                        node.RemoveChild(oldChild);
                    }
                    
// 新建一个Where,并将我们的query的主体内容赋值给其InnerXml,并在QuerNode中添该Node
                    XmlNode newChild  =  xmlDocument.CreateElement( " Where " );
                    newChild.InnerXml 
=  innerXml;
                    node.AppendChild(newChild);

                    
// 将搜索文件的类型修改为list中的所有文件和文件夹,默认为只搜索当前目录下的文件
                     if  (xmlDocument.DocumentElement.Attributes[ " Scope " ==   null )
                    {
                        XmlAttribute attScope 
=  xmlDocument.CreateAttribute( " Scope " );
                        attScope.Value 
=   " RecursiveAll " ;
                        xmlDocument.DocumentElement.Attributes.Append(attScope);
                    }
                    
else
                    {
                        xmlDocument.DocumentElement.Attributes[
" Scope " ].Value  =   " RecursiveAll " ;
                    }
                        
                        
                    
// 替换XML中的ViewEmpty部分,该部分显示没有数据的时候的内容
                    xmlDocument.DocumentElement.SelectSingleNode( " ViewEmpty " ).InnerXml  =   " <HTML><![CDATA[<font color='red'><b>No Results match keyword!</b></font>]]></HTML> " ;
                }

            }
        }


    }
}

 

转载于:https://www.cnblogs.com/ceci/archive/2009/12/02/1615592.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值