0

I am using the IN Operator in SharePoint 2010 CAML query to get my desired set of results, however, the filter criteria doesn't seem to work and returns all records instead of the filtered set.

Here's how my SharePoint List is laid out; the text in bracket shows the actual column name:

Department (Title)     Approver (Approver)
------------------    --------------------
Department 1            Bob
Department 2            Mike
Department 1            John
Department 3            Peter

Here's how my code looks like (EDITED, THIS WORKS!!):

string camlQuery = @"<View><Query><Where>
                    <In>
                        <FieldRef Name='Title' />
                        <Values> 
                            <Value Type='Text'>Department 1</Value>
                        </Values> 
                    </In> 
                    </Where></Query></View>";

string siteUrl = "http://mysharepointsite/sitename";

ClientContext _clientContext = new ClientContext(siteUrl);
List _oList = _clientContext.Web.Lists.GetByTitle(listName);

CamlQuery _camlQuery = new CamlQuery();
_camlQuery.ViewXml = camlQuery;

ListItemCollection listItemCollection = _oList.GetItems(_camlQuery);

_clientContext.Load(listItemCollection);

_clientContext.ExecuteQuery();

return listItemCollection;

It should ideally return Bob and John, but it returns all 4 approvers.

Is there anything I'm doing wrong? Thanks!

  • If the value of the field does match the filter value exactly and you have only a single filter value, why don't you use simply the Eq operator instead of In? Or is it only a simplified example of a complex solution? – pholpar Mar 10 '15 at 3:54
  • Hi pholpar, you're right it's just a simplified example of a complex solution. There might be multiple Value tags similar to how we generally use IN operator. – Anupam Mar 10 '15 at 4:33
2

You are overwriting your string _camlQuery variable that is declared at the top, when creating the CamlQuery _camlQuery = new CamlQuery();

So you are using your undeclared camlQuery here _camlQuery.ViewXml = camlQuery;

Edit:

In SharePoint CSOM the root element for CamlQuery.ViewXml property is <View>.

So changing your query to be

string camlQuery = @"<View>
                     <Query>
                     <Where>
                    <In>
                        <FieldRef Name='Title' />
                        <Values> 
                            <Value Type='Text'>Department 1</Value>
                        </Values> 
                    </In> 
                    </Where>
                    </Query>
                    </View>";
  • I assume that is only a typo, should be 'string camlQuery'. Otherwise it should be a compile-time error (double declaration of _camlQuery + undeclared camlQuery). – pholpar Mar 10 '15 at 4:11
  • Hi Robban, sorry it was a typo. Please see updated code. I have also tried with <View> but it doesn't seem to work either. – Anupam Mar 10 '15 at 4:32
  • 1
    Did you try adding the <Query></Query> around the <Where> element? – Robban1980 Mar 10 '15 at 4:35
  • Awesome! That did the trick! Thank you so much Robban!! You saved my day! :D – Anupam Mar 10 '15 at 4:37
  • how to achieve the similar kind of results for the items whose ID ranges from 100 to 200 using IN operator ? can you please help with the query ? – SaMolPP Apr 18 '18 at 20:15

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.