I am trying to retrieve only documents from a library. When I use this CAML:

<Where><Eq><FieldRef Name='FSObjType' /><Value Type='Lookup'>0</Value></Eq></Where>

I get only the documents in the root. No items form the sub folders.

when I use this:

<Where><Eq><FieldRef Name='FSObjType' /><Value Type='Lookup'>1</Value></Eq></Where>

I get only folders coming in the results.

The reason I need this is to delete all files under the document library and keep the folder structure.

any help would be much appreciated.

My folder structure for test purposes:

My Document Library

3 Answers 3


You'd need to retrieve it using Recursive All. for ex. in CSOM,

var caml = SP.CamlQuery.createAllItemsQuery();
caml.set_viewXml("<View Scope='RecursiveAll'><Query><Where><Neq><FieldRef Name='ContentType' /><Value Type='Text'>Folder</Value></Neq></Where></Query></View>");

Your two code blocks are identical. I think you might have a copy/paste error.

The usual way to get all nested documents from a CAML query is to use <View Scope='RecursiveAll'></View>

around the outside of the where clause.

  • FSObjType is 0 in the first block and 1 in the second.
    – Erin L
    Aug 18, 2016 at 18:46
  • I tried to add View, it did not change anything and I added $query.ViewAttributes = "Scope="RecursiveAll"" in my code and started to do something but still not quiet right. it should bring me only 3 results(please check my folder structure above) It also brings Folder1 in the results.
    – Aslan
    Aug 18, 2016 at 18:56
  • FSObjType of 0 and RecursiveAll for ViewScope combined has always worked for me. Post your updated code, there may be a typo somewhere.
    – NiceNix
    Aug 18, 2016 at 18:57
  • I was looking to retrieve only documents not folders. So this answer is still not complete...
    – Aslan
    Aug 18, 2016 at 18:59

Here is what I finally did to solve the problem:

I added

$query.ViewAttributes = "Scope=`"RecursiveAll`""

to my code and used this CAML Query:

<Where><Eq><FieldRef Name='FSObjType' /><Value Type='Lookup'>0</Value></Eq></Where>

Thanks for the support!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.