I am currently working with a custom web part and SharePoint 2013 XSLTListViewWebPart to show documents inside a document library (lets call it DL). I have a field called DocumentStatus inside the document library, and only documents whose DocumentStatus are 'Approved' will show up in the list view.

I also have another field called SubFolderUrl, which will be generated using workflow, and contains the relative URL of a folder (if a folder is added to the document library).

The list view works great on the root level and the first folder level of the document library. However, when a user adds a subfolder (lets call it Y) inside a folder (lets call it X), then the folders inside X (which should be Y) are not displayed on the list view web part (the files inside X are displayed correctly though).

Here is the CAML query I've thrown into the list view:

<View Scope='All' Name='MyViewName'>
    <FieldRef Name='Created' Ascending='FALSE' />
            <FieldRef Name='DocumentStatus' />
            <Value Type='Choice'>Approved</Value>
             <FieldRef Name='FileDirRef' />
             <Value Type='Lookup'>DL/X/Y</Value>
<RowLimit Paged='TRUE'>30</RowLimit>
<Aggregations Value='OFF' />
<Toolbar Type='None' />

First I thought it has something to do with the scope. But when I tried to change the scope from All to Recursive, it returned nothing. When I changed the scope to RecursiveAll, it returned folders and files inside those folders in 1 level (which is not what I expected).

Any ideas on what I am doing wrong here?

2 Answers 2


You should explicitly provide parameter to your CAML query to Get all Items inside a list (Folder/ Sub Folder /Sub Sub Folder). Use this:

            <ViewAttributes Scope="RecursiveAll" />

Edit: There is no direct way to get folders and items on demand by using one static Caml query. you have to write custom logic for this. FYI

scope ="Recursive" (fetches only Items existing at any level)

scope="RecursiveAll" (fetches Items+Folders existing at any level)

As per your requirement, You need to write custom logic about how user selects a Folder/SubFolder (viz. on click of folder you can capture folder url and then update your query). You can set Query option for Folders by putting AND condition and comparing as below:

   <FieldRef Name="FileDirRef"></FieldRef>
   <Value Type="Text">relative_url_of_folder</Value>

Part1 Part2

  • But using RecursiveAll will directly display folders, sub sub folders, and documents right? The goal here is to get all documents and folders on the root of the document library. Then if user click on a folder, the documents and sub folders inside that folder will be displayed, and so on. Commented Jun 15, 2017 at 6:50
  • Ok.. got this.. can you try with setting scope="All". I have not tested though but I think it will work. Do let me know if you get success
    – DvG
    Commented Jun 15, 2017 at 7:10
  • Refering to your updated answer, yes I do have a dynamic CAML Query and I also have the folder URL. I have tried with setting the ViewAttributes scope to All but no luck. Commented Jun 15, 2017 at 13:22
  • View Attribute has 2 parameters as listed in my edit. @AWJ Your requirement can surely be fulfilled. I don't have complete background of what you are trying to do.. Are you creating a custom page and displaying all folders & files. Or you want to run this query over LIst/Library using Script editor web part or something?
    – DvG
    Commented Jun 15, 2017 at 13:32
  • As I mentioned on my question, I am working with a custom web part, which displays list of documents and folder from a document library. If user clicks on a folder, the documents and folders inside that folder will be displayed, and so on. I can't see 2 parameters that you've mentioned in your edit. Commented Jun 15, 2017 at 13:43

I've solved this problem last week (after hours of painful research), but I am not sure what really happens and why this method worked.

So in my Web Part code behind which generate query to be passed to the XSLTListView Web Part, I changed the scope dynamically based on the location of the documents (root folder or subfolder) like:

public void LoadXsltListView()
    // other code

    if(subDirectory == string.Empty)
        scope = "All";
        scope = "RecursiveAll";

    xsltListViewWebPart.XmlDefinition = GetQuery(camlQuery, scope); // Generate full query for the xsltlistviewwebpart

    // other code

In the method that generate the full XmlDefinition, it is still unchanged, something like:

private string GetQuery(string camlQuery, string scope)
    StringBuilder sb = new StringBuilder();
    sb.Append("<View Scope='"+ scope +"'>");
    // the rest of the code

    return sb.ToString();

But again, this is SharePoint we are talking about. At least, for now, it works.

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.