How to Use CAML Query for Boolean field for SharePoint 2013/2016/Online Object Model CSOM


In this SharePoint 2013 caml query boolean field tutorial, we will discuss how to use CAML query for boolean field while working with client side object model (csom) in SharePoint 2013/2016/Online. 

We had one SharePoint Online document library where we have added one field as “IsCopied” which is of type Boolean. We wanted to query from the document library based on True/False. So we wrote the query like below:

var q = new CamlQuery() {<View><Query><Where><And><Eq><FieldRef Name=’FileLeafRef’ /><Value Type=’File’>” + docTitle + “</Value></Eq><Eq><FieldRef Name=’IsCopied’ /><Value Type=’Boolean’>TRUE</Value></Eq></And></Where></Query></View> }

But it did not return the result for us. We thought it will return results where IsCopied is TRUE.

Actually, the Boolean field in SharePoint works in 1 (TRUE) and o (FALSE).

So we have to modify the code like below:

SharePoint 2013 caml query boolean field (For True Condition)

First we will see how to use caml query in SharePoint 2013/Online for boolean field for true condition.

var q = new CamlQuery() {<View><Query><Where><And><Eq><FieldRef Name=’FileLeafRef’ /><Value Type=’File’>” + docTitle + “</Value></Eq><Eq><FieldRef Name=’IsCopied’ /><Value Type=’Boolean’>1</Value></Eq></And></Where></Query></View> }

SharePoint 2013 caml query boolean field (For False Condition)

Here we will see how to use caml query in SharePoint 2013/Online for boolean field for false condition.

var q = new CamlQuery() {<View><Query><Where><And><Eq><FieldRef Name=’FileLeafRef’ /><Value Type=’File’>” + docTitle + “</Value></Eq><Eq><FieldRef Name=’IsCopied’ /><Value Type=’Boolean’>0</Value></Eq></And></Where></Query></View> }

Now if you will query it will return the result correctly.

SharePoint 2013 caml query boolean field (Other Approach)

Instead of writting Type=’Boolean’, we can change to Type=’Bool’ then the query will work fine.

var q = new CamlQuery() {<View><Query><Where><And><Eq><FieldRef Name=’FileLeafRef’ /><Value Type=’File’>” + docTitle + “</Value></Eq><Eq><FieldRef Name=’IsCopied’ /><Value Type=’Bool’>True</Value></Eq></And></Where></Query></View> }

You may like following SharePoint CAML Query examples:

Here I have shown how to use SharePoint 2013 CAML query boolean field.

Check out Best Alternative to InfoPath -> Try Now

You May Also like the Following SharePoint Online Tutorials:

About Bijay Kumar

I am Bijay from Odisha, India. Currently working in my own venture TSInfo Technologies in Bangalore, India. I am Microsoft Office Servers and Services (SharePoint) MVP (5 times). I works in SharePoint 2016/2013/2010, SharePoint Online Office 365 etc. Check out My MVP Profile.. I also run popular SharePoint web site SharePointSky.com

View all posts by Bijay Kumar →