【问题标题】:CAML "In" Operator Equivalent For SharePoint Services 3 (SharePoint 2007)等效于 SharePoint Services 3 (SharePoint 2007) 的 CAML“In”运算符
【发布时间】:2012-06-25 22:15:49
【问题描述】:

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

【问题讨论】:

    标签: c# sharepoint-2007 caml


    【解决方案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,请使用已内置的 In Operator

    【讨论】:

    • 该死的。在花了两个小时写我的之后错过了这个。
    • 我应该更新标题或标签以便更容易找到吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多