1

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

1

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

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.