Usando Spquery con Splist.getitems (Spview)
-
10-12-2019 - |
Domanda
Ho una classe SPView
.
Inoltre, ho implementato un modulo di ricerca che quando viene inserita una parola chiave, è necessario ottenere tutti gli elementi che contengono un titolo che contiene una parte della parola chiave inserita, utilizzando SPQuery
.
L'utilizzo di SPList.GetItems()
funziona bene quando si utilizza il SPQuery
=>
var itemCollection = SPList.GetItems(SPQuery);
.
Il codice sopra mi dà gli oggetti che voglio, ma sfortunatamente anche tutti quelli che non voglio. Quello che voglio è ottenere tutti gli elementi dal SPView
che corrispondono alla parola chiave inserita.
Ho scoperto che puoi usare Spquery insieme a Spview=>
var itemCollection = SPList.GetItems(SPQuery, "viewName");
.
Che suona abbastanza bene, ma quando si è studiato più vicino rivela che il "ViewName" dovrebbe effettivamente essere il SPView
s ID ToString
ed. E se il SPQuery
contiene una clausola <Where>
, la "ViewName" sostituisce quella query e restituisce solo ogni elemento in quella particolare vista.
Quindi la mia domanda, esistono un modo di interrogare un SPView
direttamente o devo fare un lavoro doloroso in giro?
.
Soluzione Codice
Grazie, Simon Doy, per il suggerimento.
.
var query = new SPQuery(SPList.GetView(ListViewId))
{
Query = string.Format(string.Concat(
"<Where>",
"<Or>",
"<Or>",
"<Contains>",
"<FieldRef Name='Title'/>",
"<Value Type='Text'>{0}</Value>",
"</Contains>",
"<Contains>",
"<FieldRef Name='FieldAutoGeneratedName'/>",
"<Value Type='Text'>{0}</Value>",
"</Contains>",
"</Or>",
"<Contains>",
"<FieldRef Name='FieldATTCode'/>",
"<Value Type='Text'>{0}</Value>",
"</Contains>",
"</Or>",
"</Where>"
), param)
};
var items = SPList.GetItems(query);
Sopra il codice Restituisce tutti gli elementi dallo elenco specificato che corrisponde a param
Soluzione
How about creating the SPQuery object using the constructor where you can pass the appropriate SPView.
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spquery.spquery.aspx
You could then add a query to include the appropriate items, something like:-
var query = new SPQuery(spView);
query.Query = String.Format("<Where><Eq><FieldRef Name='Title'/><Value Type='Text'>{0}</Value></Eq></Where>", keyword);
SPListItemCollection listItems = oList.GetItems(query);
Altri suggerimenti
If the SPQuery returns all the items of the List, then in most cases the QueryString is simply wrong. The (in my optionion weird) default behaviour is, that in case of wrong QueryStrings a Caml-Query returns ALL the items.
For example of a common mistake: The QueryString for SPQuery.Query must NOT include the "<Query>"-Tag. If so, it returns all items and ignores the Where-Clause completely.