SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am busy messing around with lists and CAML queries:

SPQuery query = new SPQuery();
SPList list;
SPListItemCollection collection;
query = new SPQuery();
query.Query = "<Query><Where><Eq><FieldRef name=\"Title\"></FieldRef><Value Type=\"Text\">BLAH BLAH BLAH</Value></Eq></Where></Query>";
list = Answers.GetListInstance();
collection =  list.GetItems(query);
foreach (SPListItem item in collection)

I'm expecting no matches as I don't have any items with that title.. yet it returns everything. Any ideas?

share|improve this question
up vote 4 down vote accepted

Remove <Query> and </Query> from your string

share|improve this answer
when i do that it throws the error "Not all Fields are properly installed" and this is running of a default list :/ – Sebastien Stettler May 7 '12 at 13:59
And change name in FieldRef to Name – Per Jakobsen May 7 '12 at 14:01
Man we really need a basic CAML query troubleshooting wiki page... this question comes up pretty frequently. :) – Kit Menke May 7 '12 at 14:01
thanks the Name change worked. Kit i would agree with your thought :D! – Sebastien Stettler May 7 '12 at 14:03
Also, I hope you are not opening a web and then disposing it in Answers.GetListInstance() because it will be re-opened when you use the SPList... then you have a memory leak. – Kit Menke May 7 '12 at 14:03

The code below works for me. You will have to modify it to your needs.

$url = "" $web = Get-SPWeb $url

$list = $web.Lists["TestList"]

$query = New-Object -TypeName Microsoft.SharePoint.SPQuery

$query.Query = @" 2 "@

$items = $list.GetItems($query); Write-Host $items.Count

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.