在找到这个解决方案之前,我偶然发现了几种方法。我不确定它的可扩展性,但它适合我的需要,所以我想我会分享它。这个递归方法应该返回相当于
WHERE Title IN ([Title1], [Title2],...[TitleN])
对于 1-N 个字符串标题的列表。
private string _camlTitleEq = "<Eq>" +
"<FieldRef Name=\"Title\" />" +
"<Value Type=\"Text\">{0}</Value>" +
"</Eq>";
private XElement BuildOrClause(List<string> listItemTitles, int index)
{
//If we've reached the last item in the list, return only an Eq clause
if (index == listItemTitles.Count - 1)
return XElement.Parse(String.Format(_camlTitleEq, listItemTitles[index]));
else
{
//If there are more items in the list, create a new nested Or, where
//the first value is an Eq clause and the second is the result of BuildOrClause
XElement orClause = new XElement("Or");
orClause.Add(XElement.Parse(String.Format(_camlTitleEq, listItemTitles[index])));
orClause.Add(BuildOrClause(listItemTitles, index + 1));
return orClause;
}
}
你可以这样使用它:
SPQuery query = new SPQuery();
string titleIn = BuildOrClause(listItemTitles, 0).ToString(SaveOptions.DisableFormatting);
query.Query = "<Where>" +
titleIn +
"</Where>";
我希望这对仍在 SP 2007 中工作的人有所帮助。欢迎提供建设性反馈!如果您使用的是 SharePoint 2010,请使用已内置的 In Operator。