C# CAML“;在;SharePoint Services 3(SharePoint 2007)的同等运营商

C# CAML“;在;SharePoint Services 3(SharePoint 2007)的同等运营商,c#,sharepoint-2007,caml,C#,Sharepoint 2007,Caml,今天我遇到了编写CAML查询的需要,该查询使用[Values]中的WHERE[Field]。我想查询一个列表,其中列表项的标题包含在一组字符串中。在运行我的查询时收到几个错误后,我意识到In操作符是SharePoint 2010的新手,但我仍然在SharePoint 2007中。因此,唯一的选择是使用多个Or运算符。此外,Or运算符一次只能对2个值进行操作,因此需要嵌套Or运算符。如何构建这样的查询?请参阅下面我的解决方案。在使用此解决方案之前,我曾尝试过几种方法。我不确定它的可扩展性,但它适合

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

在使用此解决方案之前,我曾尝试过几种方法。我不确定它的可扩展性,但它适合我的需要,所以我想我会分享它。此递归方法应返回

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;
    }
}
private string\u camlitleq=“”+
"" +
"{0}" +
"";
私有XElement BuildOrClause(列表项标题,整数索引)
{
//如果已到达列表中的最后一项,则只返回一个Eq子句
if(index==listItemTitles.Count-1)
返回XElement.Parse(String.Format(_camlitleq,listItemTitles[index]);
其他的
{
//如果列表中有更多项,请创建新的嵌套或,其中
//第一个值是Eq子句,第二个值是BuildOrClause的结果
XElement或Clause=新XElement(“或”);
添加(XElement.Parse(String.Format(_camlitleq,listItemTitles[index]));
添加(BuildOrClause(listItemTitles,索引+1));
返回orClause;
}
}
你可以这样使用它:

SPQuery query = new SPQuery();
string titleIn = BuildOrClause(listItemTitles, 0).ToString(SaveOptions.DisableFormatting);

query.Query = "<Where>" +
                  titleIn +
              "</Where>";
SPQuery query=new SPQuery();
string titleIn=BuildOrClause(listItemTitles,0).ToString(SaveOptions.DisableFormatting);
query.query=“”+
提特林+
"";

我希望这对仍在SP 2007工作的人有所帮助。欢迎有建设性的反馈!如果您使用的是SharePoint 2010,请使用已内置的解决方案。

在使用此解决方案之前,我曾尝试过几种方法。我不确定它的可扩展性,但它适合我的需要,所以我想我会分享它。此递归方法应返回

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;
    }
}
private string\u camlitleq=“”+
"" +
"{0}" +
"";
私有XElement BuildOrClause(列表项标题,整数索引)
{
//如果已到达列表中的最后一项,则只返回一个Eq子句
if(index==listItemTitles.Count-1)
返回XElement.Parse(String.Format(_camlitleq,listItemTitles[index]);
其他的
{
//如果列表中有更多项,请创建新的嵌套或,其中
//第一个值是Eq子句,第二个值是BuildOrClause的结果
XElement或Clause=新XElement(“或”);
添加(XElement.Parse(String.Format(_camlitleq,listItemTitles[index]));
添加(BuildOrClause(listItemTitles,索引+1));
返回orClause;
}
}
你可以这样使用它:

SPQuery query = new SPQuery();
string titleIn = BuildOrClause(listItemTitles, 0).ToString(SaveOptions.DisableFormatting);

query.Query = "<Where>" +
                  titleIn +
              "</Where>";
SPQuery query=new SPQuery();
string titleIn=BuildOrClause(listItemTitles,0).ToString(SaveOptions.DisableFormatting);
query.query=“”+
提特林+
"";

我希望这对仍在SP 2007工作的人有所帮助。欢迎有建设性的反馈!如果您使用的是SharePoint 2010,请使用已内置的。

该死的。在花了两个小时写我的东西后错过了这个。我应该更新标题或标签以便更容易找到吗?该死的。在花了两个小时写我的东西后,我错过了这个。我应该更新标题或标签以便于查找吗?