结果集中包含文件夹的 CAML 查询
我正在尝试编写一个针对特定 SPList 执行的 CAML 查询,范围限定为特定文件夹,从该点递归,并返回所有 ListItems(满足条件)和文件夹。
这是查询的代码,看起来应该可以工作(为了便于阅读而格式化):
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 - 没有文件夹。
如果我从查询中删除其他条件,仅保留 FSObjType=1
,则会收到 COM 异常“无法完成此操作。请重试。”
如果我随后删除 ViewFields,只留下 Scope=RecursiveAll
和 FSObjType=1
,我会得到一个空结果集。
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();
So - this only returns the ListItems - no folders.
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."
If I then remove the ViewFields, leaving only the Scope=RecursiveAll
and FSObjType=1
, I get an empty result set back.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
每个人都很接近,但又不完全正确。
首先,使用这个FieldRef是错误的:
因为文件夹内容类型是可以继承的。因此,您需要与内容类型 ID 进行比较,如下所示:
然后,将视图属性 Scope 设置为 RecursiveAll
这应该返回内容类型继承自文件夹 (0x0120) 的任何项目
Everyone is close, but not quite right.
First of all, using this FieldRef is wrong:
because the folder content type can be inherited. Therefore, you need to compare against the content type ID, like this:
And then, set the view attribute Scope to RecursiveAll
That should return any item whose content type inherits from Folder (0x0120)
我没有可供测试的开发映像,因此我可能需要稍后修改它;但我认为您可以尝试
检索项目将允许您使用 SPUtility.GetUrlDirectory(url) 来获取给定项目的文件夹路径,并从那里解析文件夹层次结构。
I don't have my dev image to test against, so I might need to revise this later; but I think you could try
Retrieving the items will allow you to use
SPUtility.GetUrlDirectory(url)
to get the folder path for a given item, and parse the folder hierarchy from there.您可以尝试将 caml 查询基于文件夹内容类型,同时
保留
You could try basing your caml query on the Folder Content Type instead,
whilst keeping the
我已经解决了这个问题:
作为查询选项,
我在堆栈溢出上发现了关于它的问题:
如何使用 Web 服务递归地遍历共享点列表?
I've solved this putting:
As query option
I found my question about it on stack overflow:
How can I iterate recursively though a sharepoint list using webservices?
执行此操作时,您是否删除了
标签?如果不是,它将无法正确运行。无论如何,这并不能解决你的问题。您是否尝试过将查询留空?它返回什么吗?
我一直在研究类似的事情,并遇到了 问题 同样,也许有些相关。
Did you remove the
<Or>
tags when you did this? If not it will not run correctly.Regardless, that does not solve your problem. Have you tried leaving the query empty? Does it return anything?
I have been working on something similar and ran into an issue as well, perhaps it's somewhat related.
这在 SP 2010 中似乎仍然是一个问题。以下是适用于 2007 或 2010 年的解决方法代码,基于 此 MSDN 论坛帖子使用了 Web 服务:
This still seems to an issue in SP 2010. Here's workaround code that will work for 2007 or 2010, based on this MSDN Forums post that uses the web services: