11

Consider a list which has a column of type "Person or Group" (single selection) witch contains a SharePoint group. I want to return all rows for which the group is within a list. I know the IDs of the groups so I am doing this:

<Where>
  <In>
    <FieldRef Name='GroupColumn' LookupId='TRUE' />
    <Values>
      <Value Type='User'>1</Value>
      <Value Type='User'>2</Value>
      <Value Type='User'>3</Value>
    </Values>
  </In>
</Where>

This works. But how do I write the query for which the column allows multiple selections?

From what I've found it's enough to say <Value Type='UserMulti'>1</Value> instead of <Value Type='User'>1</Value>. But this does not work and throws:

Value does not fall within the expected range.

So how do I make the query for the multiple selections field?

1
  • how can i pass parameters in the code?
    – dasarp
    May 16, 2017 at 17:20

2 Answers 2

11

There is a special element which is intended for use with multiple lookup fields:

http://msdn.microsoft.com/en-us/library/ff630172.aspx

Includes Element (Query)

If the specified field is a Lookup field that allows multiple values, specifies that the Value element is included in the list item for the field that is specified by the FieldRef element.

To make it work for several ID values, you have to use "Or" logic, because "In" operator supports only strict equality comparisons (i.e. it represents "Or" of "Equal"-s).

When dealing with "Or"-s, there is known inconvenience: you have to arrange the values into pairs.

Thus the final query will look like this:

<Where>
    <Or>
        <Or>
            <Includes>
                <FieldRef Name="GroupColumn" LookupId="TRUE" />
                <Value Type="Integer">1</Value>
            </Includes>
            <Includes>
                <FieldRef Name="GroupColumn" LookupId="TRUE" />
                <Value Type="Integer">2</Value>
            </Includes>
        </Or>
        <Includes>
            <FieldRef Name="GroupColumn" LookupId="TRUE" />
            <Value Type="Integer">3</Value>
        </Includes>
    </Or>
</Where>
2
  • Thanks omlin! Just one quick question to make sure i properly understand. If I have, let's say 20 IDs in my collection, I have to pair them all by two?
    – JohnDoDo
    Jan 9, 2012 at 10:18
  • Absolutely. You can write (or find in Google, probably) a rather simple method for arranging a collection with unknown number of items into proper CAML "Or"-s. Jan 9, 2012 at 10:33
0

Eq operator also can be used with LookupMulti fields: it has different semantic in this case. I.e. if we have e.g. field of UserMulti type which contains several users we may query for user with UserId = 4 it like this:

<View>
  <Query>
    <Where>
      <Eq>
        <FieldRef Name="TestField" LookupId="True" />
        <Value Type="LookupMulti">4</Value>
      </Eq>
    </Where>
  </Query>
</View>

it will return those list items which include user with specified user id (not only those which have only this single user specified in field value as you may expect. This was one of the reasons of why support for Includes operator was not added to Camlex)

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.