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.
You could try basing your caml query on the Folder Content Type instead,
whilst keeping the
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.
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?
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.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: