Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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
add comment

2 Answers

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
add comment

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:

http://msdn.microsoft.com/en-us/library/ff798478.aspx

share|improve this answer
add comment

Your Answer

 
discard

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.