为List配置一个搜索按钮

查询前的List首页,只有一个cusotmer
customer下面有个Project文件夹和Cusotmer Complaint文件
其中Customer Complaint.txt文件是有多个属性的
现在开始针对文档库中所有的文件 和文件夹进行模糊查询
前段代码
<%@ 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>
后端代码
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/p/1615592.html