SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have good methods in place to get my core List of Posts. Let's say I get the 100 most recent Posts.

I'd like to get all comments related to those posts. Comments have a field called PostID that stores the ID of the post they are on.

I could just get all comments, and loop through them, but that seems less than ideal.

I could call comments for each post, and get them with CAML, but that's a LOT of calls.

Is there a way to build a giant CAML statement to get all posts where PostID is contained in my list of 100 posts?

share|improve this question
up vote 1 down vote accepted

You can use Linq and XElement objects to dynamically build your Caml query.

In the past, to use the "contains" clause, I have done something like the following:

public string GenerateCaml(List<int> Ids)
  XElement root = new XElement("And");
  foreach(int id in Ids)
    root.Add(new XElement("Or",
                new XElement("Eq",
                   new XElement("FieldRef",
                     new XAttribute("Name", "ID")),
                   new XElement("Value",
                     new XAttribute("Type", "Text"),
                     id.ToString()) ) );

  return root.ToString();
share|improve this answer

If you are willing to use LINQ to SharePoint, following article should help you. It covers the lookup columns at good length and also gives you examples of the CAML queries:

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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