1

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)
{
    Console.WriteLine(item["Title"]);
}

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

4

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

  • 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
  • 3
    And change name in FieldRef to Name – Per Jakobsen May 7 '12 at 14:01
  • 2
    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
0

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

$url = "http://portal.morganstanley.com" $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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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