したがって、私の問題は、CAMLとSharepointに関する知識が非常に乏しいことです。
質問:クエリ検索を作成するためにSPQueryが必要です。テキストボックスからテキストを検索します。クエリがアイテムを返すことを期待しています(たとえば、テキストボックス「Jo」と入力すると、クエリは「Johnson」という名前のすべてのアイテム、または「John」という名前などを返します)
1)TextChangedは正常に機能します。私はそれをチェックしました、大丈夫です2)SPGridViewはアイテムを大丈夫と見ます。私がgridViewに追加したSPListのアイテム-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:値が期待される範囲内にありません