[英]CAML “In” Operator Equivalent For SharePoint Services 3 (SharePoint 2007)
Today I came across the need to write a CAML query that uses WHERE [Field] IN [Values]. 今天我遇到了编写使用WHERE [Field] IN [Values]的CAML查询的需要。 I wanted to query a List where the list item's Title was contained by a collection of strings.
我想查询一个列表,其中列表项的标题包含在字符串集合中。 After receiving several errors running my query, I realized the In operator was new to SharePoint 2010 but I'm still in SharePoint 2007. As a result, the only option is to use multiple Or operators.
在收到运行我的查询的几个错误后,我意识到In运算符是SharePoint 2010的新用户,但我仍然在SharePoint 2007中。因此,唯一的选择是使用多个Or运算符。 Additionally an Or operator can only operate on 2 values at a time so this requires nested Or operators.
另外,Or运算符一次只能运行2个值,因此需要嵌套的OR运算符。 How can you build such a query?
你怎么能建立这样的查询? See my solution below.
请参阅下面的解决方案
I stumbled around a couple approaches before coming to this solution. 在找到这个解决方案之前,我偶然发现了几种方法。 I'm not sure of its scalability but it suits my needs so I thought I would share it.
我不确定它的可扩展性,但它符合我的需求,所以我想我会分享它。 This recursive method should return the equivalent of
这个递归方法应该返回相当于
WHERE Title IN ([Title1], [Title2],...[TitleN])
for a List of 1-N string titles. 对于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;
}
}
And you could use it like so: 你可以像这样使用它:
SPQuery query = new SPQuery();
string titleIn = BuildOrClause(listItemTitles, 0).ToString(SaveOptions.DisableFormatting);
query.Query = "<Where>" +
titleIn +
"</Where>";
I hope this helps someone still working in SP 2007. Constructive feedback is welcome! 我希望这可以帮助仍然在SP 2007工作的人。欢迎提供建设性的反馈! If you're in SharePoint 2010, use the already built in In Operator .
如果您在SharePoint 2010中,请使用已内置的In Operator 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.