CAML and Querying Boolean Fields
By Erkka PeltoniemiFebruary 22 201016 comments
Today I had some trouble trying to filter out items by a field of type Boolean. I have a custom content type definition which has a FieldRef to a Field:
<Field
Type="Boolean"
DisplayName="IsHeadlineDisplayName"
Description="IsHeadlineDescription"
Required="TRUE"
Group="SharepointBluesCustomFields"
ID="{3166A1DF-3EE9-4757-8344-B65BC2482261}"
SourceID="http://schemas.microsoft.com/sharepoint/3.0"
StaticName="IsHeadline"
Name="IsHeadline" />
As my first shot at trying to filter out the records I wrote this CAML query:
<Where>
<Eq>
<FieldRef Name='IsHeadline' />
<Value Type='Boolean'>TRUE</Value>
</Eq>
</Where>
As a new comer to the world of Sharepoint development I expected this query to return all of the records which have a IsHeadLine field set to true, yet when I fired the query with SPList.GetItems method the engine would happily return all of the records regardless of their IsHeadline field's value. As Sharepoint doesn't seem to be too consistent on case sensitivity I first thought this was a case sensitivity issue but the query kept returning the same results regardless of how I wrote my parameter.
Turns out you can treat the Sharepoint Boolean fields much the same way as you may have gotten used to doing with T-SQL. With Integer types. 1 representing positive and 0 a negative value.
So I modified my CAML query a bit:
<Where>
<Eq>
<FieldRef Name='IsHeadline' />
<Value Type='Integer'>1</Value>
</Eq>
</Where>
This solved my problem and now the engine is finally returning me the proper records.
this was sourced from http://www.sharepointblues.com/2010/02/22/caml-and-querying-boolean-fields/