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.
1 2 3 4 5 6 7 8 9 10 11 12 |
<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:
1 2 3 4 5 6 7 8 9 10 11 |
<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.
1 2 3 4 5 6 7 8 9 10 11 |
<Where> <And> <Eq> <FieldRef Name="OS " /> <Value Type='Text'>Windows</Value> </Eq> <IsNull> <FieldRef Name="UserName" /> </IsNull> </And> </Where> |