1

I have a list of Employee class which has empid and empname as property.

Now I want to construct a caml query IN clause where list column id in empid.

How can I write a foreach loop inside caml query and set each id in tag?

Thanks.

1
  • You need to include the SharePoint version as this is important for the available features of the CAML syntax. Jun 2, 2013 at 16:27

2 Answers 2

2

I am not sure which programming language you are using nor which version of SharePoint (2007, 20010, etc). But here is an example in C#. You should also check out Camlex.NET as Alex suggested. Here is a possible example for you, though:

    StringBuilder queryText = new StringBuilder(@"<Where><In><FieldRef Name = 'ID'/><Values>");
    foreach (var person in MyPeopleCollection)
    {
        queryText.Append(@"<Value Type = 'Number'/>");
        queryText.Append(person.Id);
        queryText.Append("</Values>");
    }
    queryText.Append(@"</In></Where>");
    SPListItemCollection Items = MyList.GetItems(new SPQuery(){QueryStringValueProvider = queryText });
2
  • Thanks Robert very much.Exactly same thing I was looking for.
    – user13408
    Jun 3, 2013 at 3:28
  • Glad I could help! Jun 3, 2013 at 12:46
1

I would suggest using Camlex.NET.

Camlex.NET simplifies creating of CAML queries for SharePoint by using expression. So you can easily construct a CAML query. Try it.

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.