CAML: Select SharePoint List Items with not empty column values
Once I needed a CAML query for selecting elements with non-empty column. I wanted to select all the users with name detected (filled) and with OS "Windows".
First I decided to use <Neq> construction and <Value Type='Text'></Value>. I thought it’s an analog for var != "". But the result was wrong.
<Where> <And> <Eq> <FieldRef Name="OS " /> <Value Type='Text'>Windows</Value> </Eq> <Neq> <FieldRef Name="UserName /> <Value Type='Text'></Value> </Neq> </And> </Where>
So I watched CAML documentation and found <IsNotNull> construction. It appeared to be right:
<Where> <And> <Eq> <FieldRef Name="OS " /> <Value Type='Text'>Windows</Value> </Eq> <IsNotNull> <FieldRef Name="UserName" /> </IsNotNull> </And> </Where>
This query returns list items where "OS" is "Windows" and UserName not null.
If you need to select elements with Null-values, you should use <IsNull> construction.
<Where> <And> <Eq> <FieldRef Name="OS " /> <Value Type='Text'>Windows</Value> </Eq> <IsNull> <FieldRef Name="UserName" /> </IsNull> </And> </Where>