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.
SharePoint Online tutorial contents:
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:
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> }
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.
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:
- Filter CAML Query by List Item ID in SharePoint Online/2013/2016
- Download, Install and Use CAML Query Helper in SharePoint Online/2013/2010/2007
- CAML Query Builder for SharePoint 2013/2016/Online
- Filter CAML Query by List Item ID in SharePoint Online/2013/2016
- CAML Query filter by date range is not working in SharePoint 2013 JavaScript client object model
- SharePoint CAML Query Order by Example with Dropdown in SharePoint Online/2013/2016
- Remove SharePoint List Title Column Programmatically using PnP CSOM
- cannot complete this action. please try again. SharePoint CAML query
- Retrieve Tasks assigned to a user or current user groups in SharePoint using CAML query
- SharePoint CAML Query Order by Example with Dropdown in SharePoint Online/2013/2016
- PowerApps Microphone Control – How to use
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.
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 EnjoySharePoint.com