包含结果集中文件夹的 CAML 查询 [英] CAML query that includes folders in result set

查看:32
本文介绍了包含结果集中文件夹的 CAML 查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写针对特定 SPList 执行的 CAML 查询,范围限定为特定文件夹,从该点开始递归,并返回所有 ListItem(满足条件)和文件夹.

I'm trying to write a CAML query that executes against a specific SPList, scoped to a specific folder, recursive from that point, and returns all ListItems (which meet a criteria) and Folders.

这是查询的代码,它看起来应该可以工作(为了可读性而格式化):

Here's the code for the query which seems like it should work (formatted for readability):

SPQuery query = new SPQuery();
query.Query = "
<Where>
    <Or>
        <Contains>
            <FieldRef Name=\"FileRef\" />
            <Value Type=\"Text\">foo</Value>
        </Contains>
        <Eq>
            <FieldRef Name=\"FSObjType\" />
            <Value Type=\"Lookup\">1</Value>
        </Eq>
    </Or>
</Where>";

query.ViewFields = "
<FieldRef Name=\"CustomField1\" Nullable=\"TRUE\" />
<FieldRef Name=\"CustomField2\" Nullable=\"TRUE\" />
<FieldRef Name=\"CustomField3\" Nullable=\"TRUE\" />
";

query.RowLimit = 500;
query.ViewAttributes = "Scope=\"RecursiveAll\"";
query.Folder = startingFolder;
DataTable dt = myList.GetItems(query).GetDataTable();

所以 - 这只会返回 ListItems - 没有文件夹.

So - this only returns the ListItems - no folders.

如果我从查询中删除其他条件,只留下 FSObjType=1,我会收到 COM 异常无法完成此操作.请重试."

If I remove the other conditions from the query, only leaving the FSObjType=1, I get a COM exception "Cannot complete this action. Please try again."

如果我然后删除 ViewFields,只留下 Scope=RecursiveAllFSObjType=1,我会得到一个空的结果集.

If I then remove the ViewFields, leaving only the Scope=RecursiveAll and FSObjType=1, I get an empty result set back.

推荐答案

每个人都很接近,但并不完全正确.

Everyone is close, but not quite right.

using (SPSite site = new SPSite("http://server/site"))
{
  SPWeb web = site.RootWeb; // See disposal guidance http://blogs.msdn.com/b/rogerla/archive/2008/10/04/updated-spsite-rootweb-dispose-guidance.aspx

  SPQuery query = new SPQuery();
  query.Query = @"
          <Where>
            <BeginsWith>
              <FieldRef Name='ContentTypeId' />
              <Value Type='ContentTypeId'>0x0120</Value>
            </BeginsWith>
          </Where>";
  query.ViewAttributes = "Scope='RecursiveAll'";
  SPList list = web.Lists[listId];
  SPListItemCollection items = list.GetItems(query);
  // Do stuff with your folders
}

首先,使用这个FieldRef是错误的:

First of all, using this FieldRef is wrong:

<FieldRef Name='ContentType' /><Value Type='Text'>Folder</Value>

因为文件夹内容类型可以继承.因此,您需要与内容类型 ID 进行比较,如下所示:

because the folder content type can be inherited. Therefore, you need to compare against the content type ID, like this:

<Where>
  <BeginsWith>
    <FieldRef Name='ContentTypeId' />
    <Value Type='ContentTypeId'>0x0120</Value>
  </BeginsWith>
</Where>

然后,将视图属性 Scope 设置为 RecursiveAll

And then, set the view attribute Scope to RecursiveAll

<View Scope='RecursiveAll'>...</View>

这应该返回其内容类型从文件夹 (0x0120) 继承的任何项目

That should return any item whose content type inherits from Folder (0x0120)

这篇关于包含结果集中文件夹的 CAML 查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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