今天我遇到了需要编写使用WHERE [现场]在[值]一个CAML查询。 我想,来查询列表项的标题载通过串集合的列表。 收到几个错误运行我的查询后,我意识到在运营商为新到SharePoint 2010,但我仍然在SharePoint 2007作为一个结果,唯一的选择是使用多个Or运算符。 另外一个或者操作者可以在2个值在一个时间仅操作所以这就要求嵌套的或运营商。 你怎么能建立这样的查询? 见下面我的解决方案。
Answer 1:
我跌跌撞撞地走着一对夫妇的方法来此解决方案之前。 我不知道它的可扩展性,但它适合我的需要,所以我想我会分享。 该递归方法应该返回相当于
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的时候,使用已经内置在操作 。
文章来源: CAML “In” Operator Equivalent For SharePoint Services 3 (SharePoint 2007)