CAML Query for SharePoint Boolean Field using Client Object Model (CSOM)

This SharePoint tutorial, we will discuss how to use CAML query for boolean fields in the SharePoint client object model (csom) for SharePoint Online site.

We had one document library where we have added one field as “IsCopied” which is of type Boolean. We wanted to query from the SharePoint 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:

CAML Query SharePoint Boolean Field for True Condition

We can write the below CAML query to check boolean field for True condition in SharePoint.

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> }

CAML Query SharePoint Boolean Field for False Condition

We can write the below CAML query to check the boolean field for False condition in SharePoint.

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.

CAML Query SharePoint Boolean Field – Another Approach

Instead of writing 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> }

Read some SharePoint CAML query examples:

In this SharePoint tutorial, we discussed how to use CAML query for SharePoint boolean field in SharePoint Online or SharePoint 2016/2013. Hope this will be helpful.

>