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.
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It only takes a minute to sign up.
Sign up to join this communityI 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.
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 });
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.