Writing CAML Queries / Examples

Further to the convenience of creating and selecting a view from the central register list to extract a CAML query, you may want to write your own query to take advantage of constructs not available via a view.

The Membership tag in CAML query allows you to filter list items based on membership within a group. This can be combined with an OR to cater for both individual names and group names.

Person/Group Column Example CAML Queries

Where current user is in a single values Person/Groups column

Copy
<Where>
<Eq>
<FieldRef Name='Project_x0020_Manager'/>
<Value Type='Integer'>
<UserID Type='Integer'/>
</Value>
</Eq>
</Where>

Where current user is a member of a groups that is listed in a person/groups column (that allows single or multiple values)

Copy
<Where>
<Membership Type='CurrentUserGroups'>
<FieldRef Name='Project_x0020_Team'/>
</Membership>
</Where>

Where current user may be named explicitly or may be a member of a group listed in a person/groups column (that allows single or multiple values)

Copy
<Where>
<Or>
<Membership Type='CurrentUserGroups'>
<FieldRef Name='Project_x0020_Team'/>
</Membership>
<Includes>
<FieldRef Name='Project_x0020_Team'/>
<Value Type='Integer'>
<UserID Type='Integer'/>
</Value>
</Includes>
</Or>
</Where>

Where the Project Status (choice type) column has a status of "Open"

AND Where current user may be named explicitly or may be a member of a group listed in the "Project Team" person/groups column (that allows single or multiple values) OR Where current user is explicitly named in the single value "Project Manager" Person/Groups column

Copy

<Where>
<And>
<Eq>
<FieldRef Name='Project_x0020_Status' />
<Value Type='Choice'>Open</Value>
</Eq>
<Or>
<Eq>
<FieldRef Name='Project_x0020_Manager' />
<Value Type='Integer'>
<UserID Type='Integer' />
</Value>
</Eq>
<Or>
<Membership Type='CurrentUserGroups'>
<FieldRef Name='Project_x0020_Team'/>
</Membership>
<Includes>
<FieldRef Name='Project_x0020_Team'/>
<Value Type='Integer'>
<UserID Type='Integer'/>
</Value>
</Includes>
</Or>
</Or>
</And>
</Where>