CAML query on GetItems doesn’t filter the content

So basically today I am creating a SPQuery object to filter list items. The filtering code for list goes like this

SPQuery qry = new SPQuery();
//..
//.. generate the query here
//..
SPListItemCollection listItemsCollection = list.GetItems(qry);

The code doesn’t give any error, but somehow the CAML query doesn’t work. Kind of misleading isn’t it 🙂

So it turns out that the CAML that I generate is wrong, I use <query> tag that makes it ignore all of my query entirely (somehow it doesn’t give me any error at all).

<Query>
<Where>
<Or>
<Geq><FieldRef Name="Expires" />
<Value IncludeTimeValue="TRUE" Type="DateTime">2011-05-06T21:00:21Z</Value></Geq>
<IsNull><FieldRef Name="Expires" /></IsNull>
</Or>
</Where>
<OrderBy><FieldRef Name="Created" Ascending="False" /></OrderBy></Query>

Just remove the query tag and the GetItems should work

<Where><Or><IsNull><FieldRef Name="Expires" /></IsNull>
<Geq><FieldRef Name="Expires" /><Value Type="DateTime">2011-05-06T14:22:09</Value></Geq></Or>
</Where>
<OrderBy><FieldRef Ascending="FALSE" Name="Created" /></OrderBy>
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s