Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/sharepoint/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sharepoint SPQuery搜索不支持';“我不能和”一起工作;以“开始”;_Sharepoint_Textchanged_Spgridview_Splist_Spquery - Fatal编程技术网

Sharepoint SPQuery搜索不支持';“我不能和”一起工作;以“开始”;

Sharepoint SPQuery搜索不支持';“我不能和”一起工作;以“开始”;,sharepoint,textchanged,spgridview,splist,spquery,Sharepoint,Textchanged,Spgridview,Splist,Spquery,所以,我的问题是我对CAML和Sharepoint的了解非常差 问题:我需要SPQuery用于构建查询搜索,搜索文本框中的文本。我希望我的查询返回我的项目(例如,我在文本框中键入“Jo”,然后查询返回我姓“Johnson”或名为“John”的所有项目等) 1) 文本更改工作正常。我查过了,没问题 2) SPGridView可以查看项目。我将SPList中的项目添加到gridView中-gridView可以查看这些项目。 3) 但我的查询不起作用。请提供链接/建议帮助 public class

所以,我的问题是我对CAML和Sharepoint的了解非常差

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

1) 文本更改工作正常。我查过了,没问题 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;
    }
System.ArgumentException:值不在预期范围内 射程


您是否调试了代码以检查生成的查询字符串?此外,名称开关下还有query=query=


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

您必须在查询的视图字段中包含字段姓氏:

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

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

是的,我已经调试了下面的代码(只想检查mu查询):SPListItemCollection items=list.GetItems(查询);foreach(items中的SPListItem){searchTextBox.Text+=item[“姓氏”]+“”;}和我有错误:System.ArgumentException:值不在预期范围内。好的,请运行测试并附加调试器。在出现错误之前,提取字符串变量查询并将其显示给我。另外,确定它是在list.GetItems(query)行上还是在foreach上中断。因此,这是我的查询:Pet和no,它在getItemsReady(have)spQuery.ViewFields=“”上没有中断,但仍然没有成功
SPQuery query = // ...
query.ViewFields = "<FieldRef Name='Surname' />";
// ...