CAML query for boolean column is not working
Asked Answered
A

3

20

Hi I have a SharePoint list to be queried for my Desktop App and I want to retrieve only the Active Members but when I queried I got only the users who aren't active. What is wrong with my CAML query?

camlQuery.ViewXml = "<<"View">><Query><Where><Eq><FieldRef Name='Active'/><Value Type='Boolean'> " + true + "</Value></Eq></Where></Query></View>"";

I tried the following as well

camlQuery.ViewXml = "<View><Query><Where><Eq><FieldRef Name='Active'/><Value Type='Boolean'> true</Value></Eq></Where></Query></View>";

and

camlQuery.ViewXml = "<Query><Where><Eq><FieldRef Name='Active'/><Value Type='Boolean'> true</Value></Eq></Where></Query>";

Please help as I'm new to CAML.

Anorexia answered 27/6, 2012 at 5:11 Comment(2)
Try to search in google, there is a lot samples of caml query in sharepoint. Things that you have written doesn't look like caml queryContrapuntal
For some reason that I don't know, I'm sure that sharepoint has a bug, because I have two boolean columns configured by the same way (column "Returned" and column "Checked") and if I use a CAML code to query the spList by the column "Returned" it works, but when I JUST change this CAML code to query by the other column "Checked", just changing the attribute name to the column "Checked", where we set the internal field name only, the code doesn't bring me any spListitem as result, but it should return some items. very weird Column "A" (Returned) <Where><Eq><FieldRef Name='Returned'/><Value TypeCandra
M
50

Save yourself some grief and use a tool to help build up CAML queries such as U2U's Caml Query Builder.

You need to use 1 and 0 (not true and false) in the query, so

<Query><Where>
   <Eq><FieldRef Name="Active" /><Value Type="Boolean">1</Value></Eq>
</Where></Query>
Marci answered 27/6, 2012 at 7:59 Comment(2)
Weirdly enough, for some Boolean columns you have to put value as "true" and not "1" - I am yet to figure out whyDwelt
If you keep Type="Integer", you will always have to use 1 and 0Saucedo
R
2

This Works for me

camlQuery.ViewXml = "<View>" + "<Query>" + "<Where>" + "<Eq>" +
"<FieldRef Name='Active'/>" + " <Value Type='Boolean'>" + "1" + "</Value>" +
"</Eq>" + "</Where>" + "</Query>" + "</View>";
Runofthemine answered 25/3, 2014 at 16:59 Comment(2)
Why do you concatenate the query string like this?Gonidium
@Gonidium I was taught to do it this way.Runofthemine
S
1

Use the value type Bool and it works with "true", "True" or "TRUE"

Storer answered 10/6, 2013 at 11:34 Comment(3)
I do't think such type exists, can you post link to documentation showing it?Copula
Just use this method via Caml Query Builder and it work just fine!Copycat
Heh, it really works. And looks like it is a better option as it takes 1, true, True and TRUE while Boolean only 1. Tested on SP OnlineWhisk

© 2022 - 2024 — McMap. All rights reserved.