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?

2 Answers 2

4

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

8
  • when i do that it throws the error "Not all Fields are properly installed" and this is running of a default list :/ May 7, 2012 at 13:59
  • 3
    And change name in FieldRef to Name May 7, 2012 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, 2012 at 14:01
  • thanks the Name change worked. Kit i would agree with your thought :D! May 7, 2012 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, 2012 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 and acknowledge you have read our privacy policy.

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