CorasWorks support for In, Includes and NotIncludes filters in SharePoint

Starting with SharePoint 2010, three new CAML filter operators were added, but scant details and examples mean that even 3+ years later, few people know about or use them. Fortunately, the CorasWorks platform (including CAPS) fully support these new operators – and here’s some insight on how you can take advantage of them!

The <In> operator

This is probably my favorite addition of the three and the one I use most often; what it does is allow you to collapse what would normally be a string of multiple <Or> filters into one single <In> condition.

For example, you want to filter a native Tasks list to show all items that are “Not Started”, “In Progress” or “Waiting on Someone Else”; you would likely string three together three <Eq> conditions inside <Or> operators (or even string two <Neq> filters for the other possible values of “Completed” & “Deferred”).  Something like this:

<Where>
<Or>
<Or>
<Eq>
<FieldRef Name=”Status”/>
<Value Type=”Choice”>Not Started</Value>
</Eq>
<Eq>
<FieldRef Name=”Status”/>
<Value Type=”Choice”>In Progress</Value>
</Eq>
</Or>
<Eq>
<FieldRef Name=”Status”/>
<Value Type=”Choice”>Waiting on Someone Else</Value>
</Eq>
</Or>
</Where>

Now let’s see that same filter written using the <In> operator:

<Where>
<In>
<FieldRef Name=”Status”/>
<Values>
<Value Type=”Choice”>Not Started</Value>
<Value Type=”Choice”>In Progress</Value>
<Value Type=”Choice”>Waiting on Someone Else</Value>
</Values>
</In>
</Where>

How’s that for efficiency? Easier to read and easier to write – bet you never thought you’d say that about a CAML query.

The <Includes> operator

If the <In> operator is akin to a string of <Or> conditions on a single value, the <Includes> operator is a specialized option for working with multi-select Lookup values; think of it as a “contains”, but intelligent enough to differentiate if you have similar text in multiple Lookup values.

Sticking with our Task list example, imagine wanting to filter by the multi-select Lookup that is the Predecessors column to find all Tasks that have a specific predecssor – this would allow you to see all the Tasks that might be impacted by a specific one slipping. You can’t use an <Eq> condition because that would only bring back items whose only predecessor is the Task you want – again, the <Includes> gives you the advantage of working more like an intelligent contains, like such:

<Where>
<Includes>
<FieldRef Name=’Predecessors’/>
<Value Type=’Lookup’>Task 4</Value>
</Includes>
</Where>

The <NotIncludes> operator

This one is now simple because it’s just the inverse of <Includes>. Still specifically built for multi-select Lookups, it would allow you to filter for, in our same scenario, all Tasks that do not have a given Task as a predecessor – it’s the intelligent version of a “does not contain” filter, implemented as:

<Where>
<NotIncludes>
<FieldRef Name=’Predecessors’/>
<Value Type=’Lookup’>Task 4</Value>
</NotIncludes>
</Where>

References

Not that there’s much more detail there, but here are the corresponding MSDN links for these operators as well:

Comments are closed.