CAML "NOT IN" query
Asked Answered
R

4

8

Is there a way to do something like "NOT IN" behavior in SharePoint 2010? I can easily implement IN behavior like that:

<Where>
   <In>
      <FieldRef Name="ID"/>
      <Values>
         <Value Type="Counter">1</Value>
         <Value Type="Counter">2</Value>
         <Value Type="Counter">3</Value>
         <Value Type="Counter">4</Value>
         <Value Type="Counter">5</Value>
      </Values>
   </In>
</Where>

But is there a way to select all the values that DOES NOT IN Values enumeration?

Here is the USE CASE: I have a Lookup field with AllowMultipleValues = true, and I need to get all items from LookupList, which are not included into Lookup field

Thanks in advance!

Royall answered 20/7, 2012 at 13:30 Comment(0)
Q
2

Starting from SharePoint 2010, there's the NotIncludes element that might work for you. From MSDN:

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

Template:

<NotIncludes>
    <FieldRef Name="Field_Name" />
    <Value Type="Field_Type" />
    <XML />
</NotIncludes>
Quarrel answered 2/1, 2014 at 14:32 Comment(0)
R
1

To get the opposite behavior of 'In', you have to make a nested 'Neq' query. 'NotIncludes' could be substituted for or combined with 'Neq' if you are dealing with a Lookup Field with multiple values.

<Query>
    <Where>
        <And>
            <And>
                <Neq>
                    <FieldRef Name="ID" /><Value Type="Counter">5</Value>
                </Neq>
                <Neq>
                    <FieldRef Name="ID" /><Value Type="Counter">13</Value>
                </Neq>
            </And>
            <And>
                <NotIncludes>
                    <FieldRef Name="children" /><Value Type="Lookup">20</Value>
                </NotIncludes>
                <NotIncludes>
                    <FieldRef Name="children" /><Value Type="Lookup">32</Value>
                </NotIncludes>
            </And>
        </And>
    </Where>
</Query>

If you want more variables then more nesting needs to be done. Have fun.

Repose answered 30/4, 2015 at 16:44 Comment(0)
B
0

I think the Not equal option would be the best way to build this CAML query

Maybe this (didn't test this, so bear with me)

<Query>
   <Where>
     <And>
       <Neq>
          <FieldRef Name="ID" /><Value Type="Counter">1</Value>
       </Neq>
       <Neq>
          <FieldRef Name="ID" /><Value Type="Counter">2</Value>
       </Neq>
       <Neq>
          <FieldRef Name="ID" /><Value Type="Counter">3</Value>
       </Neq>
     </And>
   </Where>
</Query>

You should have a look at the available Comparison Operators

  • Contains
  • BeginsWith
  • Eq, Equal
  • Neq, Not equal
  • Gt, Greater than
  • Lt, Less than
  • Geq, Greater than or equal to
  • Leq, Less than or equal to
  • DateRangesOverlap, Compare dates in recurring event with specified value
  • IsNotNull
  • IsNull
Beanpole answered 20/7, 2012 at 20:2 Comment(3)
I don't think that CAML is valid as is - you can't have more than 2 children under AND IIRC.Outsoar
Alright, I'll perform some tests and come back with the result when done!Beanpole
Thank you all for the answer, but in case of using <And> operators query becomes a little ugly when the count of possible variants more than 10 (for ex.), by the way, I've solved this problem using <And> logic for now, but I think that it would be great if it will be possible to implement <NotIn> logic somehow...Royall
S
0

The closest I've come is to use <NotIncludes></NotIncludes>, but for some reason is does not work the same way as <In></In>. In <In> I can use <Values>. With <NotIncludes> it looks like you can only specify one value. The rest will have to be <Or>ed in, similar to <Neq>.

Surprint answered 3/12, 2013 at 12:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.