CAML“输入"SharePoint Services 3 (SharePoint 2007) 的等效运算符 [英] CAML "In" Operator Equivalent For SharePoint Services 3 (SharePoint 2007)

查看:52
本文介绍了CAML“输入"SharePoint Services 3 (SharePoint 2007) 的等效运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天我发现需要编写一个使用 WHERE [Field] IN [Values] 的 CAML 查询.我想查询一个列表,其中列表项的标题包含在字符串集合中.在运行查询时收到几个错误后,我意识到 In 运算符是 SharePoint 2010 的新功能,但我仍在使用 SharePoint 2007.因此,唯一的选择是使用多个 Or 运算符.此外, Or 运算符一次只能对 2 个值进行操作,因此这需要嵌套的 Or 运算符.您如何构建这样的查询?请参阅下面的我的解决方案.

Today I came across the need to write a CAML query that uses WHERE [Field] IN [Values]. 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. Additionally an Or operator can only operate on 2 values at a time so this requires nested Or operators. 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]) 

用于 1-N 个字符串标题的列表.

for a List of 1-N string titles.

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.

I hope this helps someone still working in SP 2007. Constructive feedback is welcome! If you're in SharePoint 2010, use the already built in In Operator.

这篇关于CAML“输入"SharePoint Services 3 (SharePoint 2007) 的等效运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆