Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

up vote 3 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
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

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

share|improve this answer

Your Answer

 
discard

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.