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?

  • how can i pass parameters in the code? – prasadwt May 16 '17 at 17:20
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>
  • 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 '12 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. – Andrey Markeev Jan 9 '12 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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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