SPQuery 搜索不适用于“BeginsWith”

发布于 2024-12-27 06:12:13 字数 5944 浏览 0 评论 0原文

所以,我的问题是我对 CAML 和 Sharepoint 的了解很差。

问题:我需要 SPQuery 来构建查询搜索,搜索文本框中的文本。我希望我的查询返回我的项目(例如,我在文本框“Jo”中键入,查询返回我姓氏“Johnson”或姓名“John”等的所有项目)

1)TextChanged 工作正常。我检查过了,没有问题 2) SPGridView 可以正常查看项目。我将 SPList 中的项目添加到 gridView - 可以通过 gridview 查看。 3)但是我的查询不起作用。请帮助提供链接/建议

 public class VisualWebPart1 : WebPart
{
    SPSite site;
    SPWeb web;
    SPGridView gridView;
    SPDataSource dataSource;
    TextBox searchTextBox;
    UpdatePanel panel;
    SPList list;
    SPList resultList;

    string currentList;
    // Visual Studio might automatically update this path when you change the Visual Web Part project item.
    private const string _ascxPath = @"~/_CONTROLTEMPLATES/CRMSearchWebPart/VisualWebPart1/VisualWebPart1UserControl.ascx";

    protected override void CreateChildControls()
    {
        gridView = new SPGridView();
        searchTextBox = new TextBox();
        panel = new UpdatePanel();

        searchTextBox.AutoPostBack = true;
        searchTextBox.Visible = true;
        searchTextBox.Enabled = true;
        searchTextBox.TextChanged += new EventHandler(searchTextBox_TextChanged);

        gridView.Visible = true;
        gridView.Enabled = true;
        gridView.AutoGenerateColumns = false;
        AddColumnToSPGridView("Surname", "Surname");

        panel.UpdateMode = UpdatePanelUpdateMode.Conditional;
        panel.ContentTemplateContainer.Controls.Add(searchTextBox);
        panel.ContentTemplateContainer.Controls.Add(gridView);


        Control control = Page.LoadControl(_ascxPath);
        Controls.Add(control);

        Controls.Add(panel);
    }

    protected override void Render(HtmlTextWriter writer)
    {
        panel.RenderControl(writer);
    }

    //Open WebSite with List "listName"
    private void OpenWebSite(string listName)
    {
        site = SPContext.Current.Site;
        web = site.OpenWeb();
        list = web.Lists[listName];
    }

    //Add Column to gridView
    private void AddColumnToSPGridView(string HeaderText, string Datafield)
    {
        SPBoundField boundField = new SPBoundField();
        boundField.HeaderText = HeaderText;
        boundField.DataField = Datafield;
        gridView.Columns.Add(boundField);
    }

    //Build query for search; fieldName - Name of column of current List, searchQuery - our query
    private string BuildQuery(string fieldRefName, string searchQuery)
    {
        string query = "";
        switch (fieldRefName)
        {
            case "Surname":
                query = "<Where><BeginsWith><FieldRef Name='Surname'/>" +
            "<Value Type=Text>"+searchQuery+"</Value></BeginsWith></Where>";
                break;
            case "Name":
                query = query = "<Where><BeginsWith><FieldRef Name='Name'/>" +
            "<Value Type=Text>"+searchQuery+"</Value></BeginsWith></Where>";
                break;
            case "PassportNumber":
                query = "<Where><BeginsWith><FieldRef Name='PassportNumber'/>" +
            "<Value Type=Text>"+searchQuery+"</Value></BeginsWith></Where>";
                break;
            default: break;
        }
        return query;
    }

    // search in List for selected items and returns SPGridView
    private void searchTextBox_TextChanged(object sender, EventArgs e)
    {
        dataSource = new SPDataSource();
        string querySearch = searchTextBox.Text;

        OpenWebSite("Surnames");

        string query = BuildQuery("Surname", querySearch);
        SPQuery spQuery = new SPQuery();
        spQuery.ViewFields = "<FieldRef Name = 'Title'/><FieldRef Name = 'Surname'/><FieldRef Name = 'Name'/>";
        spQuery.Query = query;

        SPListItemCollection items = list.GetItems(query);
        foreach (SPListItem item in items)
        {
            searchTextBox.Text += item["Surname"] + " ";
        }

        //resultList = web.Lists["TempSurnames"];
        //resultList = AddItemsToSPList(resultList, items);
        BindDataSource(dataSource, resultList);
        //resultList = AddSPList("Result2", "Result list");
        //resultList = AddItemsToSPList(resultList, items);
        list = AddItemsToSPList(list, items);
        //BindDataSource(dataSource, resultList);
        BindDataSource(dataSource, list);
        BindGridView(gridView, dataSource);

        //var item = list.Items[3];
        //var item = resultList.Items[1];
        //searchTextBox.Text = item["Surname"].ToString();
        //resultList.Delete();
    }

    //Binds datasource of existing gridView with SPDataSource
    private void BindGridView(SPGridView gridview, SPDataSource datasource)
    {
        gridview.DataSource = datasource;
        gridview.DataBind();
    }

    //Add SPListItem items to SPList
    private SPList AddItemsToSPList(SPList spList, SPListItemCollection collection)
    {
        foreach (SPListItem item in collection)
        {
            var listItem = spList.AddItem();
            listItem = item;
        }
        return spList;
    }

    //Binds existing SPDataSource to SPList
    private void BindDataSource(SPDataSource spDataSource, SPList spList)
    {
        spDataSource.List = spList;
    }

    private SPList AddSPList(string listName, string listDescription)
    {
        OpenWebSite("Surnames");
        SPListCollection collection = web.Lists;
        collection.Add(listName, listDescription, SPListTemplateType.CustomGrid);
        resultList = web.Lists[listName];
        return resultList;
    }

更新:

这部分给了我一个错误:

SPListItemCollection items = list.GetItems(query); 
foreach (SPListItem item in items) 
{ 
  searchTextBox.Text += item["Surname"] + " "; 
} 

System.ArgumentException:值未落在预期范围内 范围

So, my problem is that my knowledge of CAML and Sharepoint is very poor.

Question: I need SPQuery for building query search, search text I have from textbox. I expect that my query returns me item(s) (for example, I type in textbox "Jo" and query returns me all items with surname "Johnson", or name "John", etc)

1)TextChanged works ok. I've check it, there is ok
2) SPGridView views items ok. Items from SPList I add to gridView - there are viewd by gridview.
3) But my query doesn't work. Please, help with links/advises

 public class VisualWebPart1 : WebPart
{
    SPSite site;
    SPWeb web;
    SPGridView gridView;
    SPDataSource dataSource;
    TextBox searchTextBox;
    UpdatePanel panel;
    SPList list;
    SPList resultList;

    string currentList;
    // Visual Studio might automatically update this path when you change the Visual Web Part project item.
    private const string _ascxPath = @"~/_CONTROLTEMPLATES/CRMSearchWebPart/VisualWebPart1/VisualWebPart1UserControl.ascx";

    protected override void CreateChildControls()
    {
        gridView = new SPGridView();
        searchTextBox = new TextBox();
        panel = new UpdatePanel();

        searchTextBox.AutoPostBack = true;
        searchTextBox.Visible = true;
        searchTextBox.Enabled = true;
        searchTextBox.TextChanged += new EventHandler(searchTextBox_TextChanged);

        gridView.Visible = true;
        gridView.Enabled = true;
        gridView.AutoGenerateColumns = false;
        AddColumnToSPGridView("Surname", "Surname");

        panel.UpdateMode = UpdatePanelUpdateMode.Conditional;
        panel.ContentTemplateContainer.Controls.Add(searchTextBox);
        panel.ContentTemplateContainer.Controls.Add(gridView);


        Control control = Page.LoadControl(_ascxPath);
        Controls.Add(control);

        Controls.Add(panel);
    }

    protected override void Render(HtmlTextWriter writer)
    {
        panel.RenderControl(writer);
    }

    //Open WebSite with List "listName"
    private void OpenWebSite(string listName)
    {
        site = SPContext.Current.Site;
        web = site.OpenWeb();
        list = web.Lists[listName];
    }

    //Add Column to gridView
    private void AddColumnToSPGridView(string HeaderText, string Datafield)
    {
        SPBoundField boundField = new SPBoundField();
        boundField.HeaderText = HeaderText;
        boundField.DataField = Datafield;
        gridView.Columns.Add(boundField);
    }

    //Build query for search; fieldName - Name of column of current List, searchQuery - our query
    private string BuildQuery(string fieldRefName, string searchQuery)
    {
        string query = "";
        switch (fieldRefName)
        {
            case "Surname":
                query = "<Where><BeginsWith><FieldRef Name='Surname'/>" +
            "<Value Type=Text>"+searchQuery+"</Value></BeginsWith></Where>";
                break;
            case "Name":
                query = query = "<Where><BeginsWith><FieldRef Name='Name'/>" +
            "<Value Type=Text>"+searchQuery+"</Value></BeginsWith></Where>";
                break;
            case "PassportNumber":
                query = "<Where><BeginsWith><FieldRef Name='PassportNumber'/>" +
            "<Value Type=Text>"+searchQuery+"</Value></BeginsWith></Where>";
                break;
            default: break;
        }
        return query;
    }

    // search in List for selected items and returns SPGridView
    private void searchTextBox_TextChanged(object sender, EventArgs e)
    {
        dataSource = new SPDataSource();
        string querySearch = searchTextBox.Text;

        OpenWebSite("Surnames");

        string query = BuildQuery("Surname", querySearch);
        SPQuery spQuery = new SPQuery();
        spQuery.ViewFields = "<FieldRef Name = 'Title'/><FieldRef Name = 'Surname'/><FieldRef Name = 'Name'/>";
        spQuery.Query = query;

        SPListItemCollection items = list.GetItems(query);
        foreach (SPListItem item in items)
        {
            searchTextBox.Text += item["Surname"] + " ";
        }

        //resultList = web.Lists["TempSurnames"];
        //resultList = AddItemsToSPList(resultList, items);
        BindDataSource(dataSource, resultList);
        //resultList = AddSPList("Result2", "Result list");
        //resultList = AddItemsToSPList(resultList, items);
        list = AddItemsToSPList(list, items);
        //BindDataSource(dataSource, resultList);
        BindDataSource(dataSource, list);
        BindGridView(gridView, dataSource);

        //var item = list.Items[3];
        //var item = resultList.Items[1];
        //searchTextBox.Text = item["Surname"].ToString();
        //resultList.Delete();
    }

    //Binds datasource of existing gridView with SPDataSource
    private void BindGridView(SPGridView gridview, SPDataSource datasource)
    {
        gridview.DataSource = datasource;
        gridview.DataBind();
    }

    //Add SPListItem items to SPList
    private SPList AddItemsToSPList(SPList spList, SPListItemCollection collection)
    {
        foreach (SPListItem item in collection)
        {
            var listItem = spList.AddItem();
            listItem = item;
        }
        return spList;
    }

    //Binds existing SPDataSource to SPList
    private void BindDataSource(SPDataSource spDataSource, SPList spList)
    {
        spDataSource.List = spList;
    }

    private SPList AddSPList(string listName, string listDescription)
    {
        OpenWebSite("Surnames");
        SPListCollection collection = web.Lists;
        collection.Add(listName, listDescription, SPListTemplateType.CustomGrid);
        resultList = web.Lists[listName];
        return resultList;
    }

Update:

This part gives me an error:

SPListItemCollection items = list.GetItems(query); 
foreach (SPListItem item in items) 
{ 
  searchTextBox.Text += item["Surname"] + " "; 
} 

System.ArgumentException: Value does not fall within the expected
range

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你げ笑在眉眼 2025-01-03 06:12:13

您必须将字段 Surname 包含到查询的视图字段中:

SPQuery query = // ...
query.ViewFields = "<FieldRef Name='Surname' />";
// ...

您可以将视图字段理解为 SQL 查询的 SELECT 部分。

You have to include the field Surname into the view fields of the query:

SPQuery query = // ...
query.ViewFields = "<FieldRef Name='Surname' />";
// ...

You can understand the view fields like the SELECT part of a SQL query.

沧桑㈠ 2025-01-03 06:12:13

您是否调试过代码以检查生成的查询字符串?此外,您还可以在“名称”开关下找到“query = query =”。

另外,您知道该开关区分大小写,因此请确保正确输入 searchQuery 选项。

Have you debugged your code to check the query string that is generated? Also, you have query = query = under the Name switch.

Also, you know that the switch is case sensitive, so ensure you enter your searchQuery option appropriately.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文