CAML and Querying Boolean Fields
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:
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:
<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:
<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.
Popularity: 6% [?]
This is the same problem I was having. Changing “Boolean” to “Integer” got what I needed.
Thanks for the tip!
Great.. This helped me a lot.
If you have used “Bool” instead of “Boolean”, everything would be ok.
This saved my time. Thanks Erkka
Yep, I verified what Rufat says. You can use TRUE and it’ll work.
That was supposed to say <Value Type=”Bool”>TRUE</Value>
For anyone else, if you use "<" and ">" here, you’ll need to do it with apersand-l-t-semicolon and apersand-g-t-semicolon.
“ampersand”, I mean… it’s been a long day…
“Great.. This helped me a lot.”
This worked in my situation only if I used the reverse:
1
I’m wondering if the default value is really a NULL and that’s why the positive version, value = 0, did not work.
Anyone know the CAML specification for a NULL value?
that’s : ” 1″
Thanks! Useful post!
Thank you!!! Useful and solved an issue I have been dealing with for quite some time.
Thanks Erkka,
It was exactly what I needed.
This works too: 1
This works too: <Value Type=’Boolean’>1</Value>
[...] seen in the post CAML and Querying Boolean Fields, if you need to query a boolean field, don't cast it as [...]
Using Bool solved my problem!
Thanks
Good Site, Carry on the beneficial work. thnx!.
Thanks for a marvelous posting! I seriously enjoyed reading it, you can be a great author.I will be sure to bookmark your blog and will come back someday. I want to encourage one to continue your great work, have a nice afternoon!