SharePoint list CAML query using CONTAINS
Asked Answered
J

1

13

I'm trying to query a SharePoint list using the following CAML query in a webpart. I have tested the query in U2U CAML Query Builder and Stramit CAML Viewer and it works fine, only returning the matching records, but when I use it in my webpart it return all list items. It is driving me crazyyyyy. Here is the code:

string camlQuery = string.Format(@"<Query><Where><Contains><FieldRef Name='Title' /><Value Type='Text'>2</Value></Contains></Where></Query>");

SPQuery query = new SPQuery();
query.Query = camlQuery;
SPListItemCollection items = Articles.GetItems(query);
grid.DataSource = items.GetDataTable();
grid.DataBind();
Januarius answered 8/12, 2009 at 22:25 Comment(0)
M
25

Leave out the surrounding Query tag, just use:

<Where><Contains><FieldRef Name='Title' /><Value Type='Text'>2</Value></Contains></Where>

SPQuery adds the Query tag itself.

Michi answered 8/12, 2009 at 22:58 Comment(1)
I know... :-P. glad to be of help.Michi

© 2022 - 2024 — McMap. All rights reserved.