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?

2 Answers 2


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();

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:


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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